Why are two different numbers equal in JavaScript?

All numbers in JavaScript are internally represented by 64-bit floating point numbers (see §4.3.19 of the specification). That means it can exactly represent every integer from 0 up to 9007199254740992 (hex value 0x20000000000000). Any integers greater than that (or less than it’s negative counterpart) may need to be rounded to the closest approximate value. Observe: … Read more

Best way to restrict a text field to numbers only?

This is something I made another time for just numbers, it will allow all the formatters as well. jQuery $(‘input’).keypress(function(e) { var a = []; var k = e.which; for (i = 48; i < 58; i++) a.push(i); if (!(a.indexOf(k)>=0)) e.preventDefault(); });​ Try it http://jsfiddle.net/zpg8k/ As a note, you’ll want to filter on submit/server side … Read more

Remove leading zeros from input type=number

I’m going to give an example with react usage. It uses state which store the value of the field. But, I think u can replace it with whatever variable you like. <input type=”number” value={Number(this.state.myNumber).toString()}/> In my case, myNumber stores a year number. This way, the year 2018 (for example) won’t be displayed mistakenly as 02018.