-2

So right now the user is able to insert a decimal value in the number field of the HTML5 input type="number". I want to prevent that, so if a user types "5.1" it will insert "51" in the textbox.

Is there any way to do this? Thanks!

Gaspa79
  • 5,488
  • 4
  • 40
  • 63
  • 5
    You have a very similar question here: http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input – nanocv May 14 '16 at 23:56

2 Answers2

3

The definite answer is this:

function preventDot(e) {
            var key = e.charCode ? e.charCode : e.keyCode;

            if (key == 46) {
                e.preventDefault();
            }
        }

And bind it on keydown

Gaspa79
  • 5,488
  • 4
  • 40
  • 63
0

You can use a regular expression to find the characters that make the value a decimal value, and then delete them so that the value internally becomes an integer:

/[\.,]/
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27
  • There's a simple reason for this: In my home country the period doesn't mean "decimal place" but "thousands place". Therefore, a user will type 7.900 and it will parse it as 7, which would be wrong – Gaspa79 May 14 '16 at 23:55