Prevent typing non-numeric in input type number

Explosion Pills

Using <input type=number> will cause this.value inside of an event listener to return an empty string if the input is not a valid number. You can see an example of this at http://jsfiddle.net/fSy53/

However, the invalid characters are still displayed in the input.

Is there any way to get the value that is actually displayed, including the invalid characters, from within an event listener?

My ultimate goal is to prevent users from actually typing any non-numeric characters into the field. I need to use type=number so that the numeric virtual keyboard is used by mobile devices. My goal would be to do something like this.value = this.value.replace(/[^0-9.]/g, "") on keyup keypress, but this doesn't work because if an invalid character is typed, reading from this.value returns "".

Glenn Lane

Try preventing the default behaviour if you don't like the incoming key value:

document.querySelector("input").addEventListener("keypress", function (evt) {
    if (evt.which < 48 || evt.which > 57)
    {
        evt.preventDefault();
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HTML5 input type=number value is empty in Webkit if has spaces or non-numeric characters?

From Dev

Prevent negative inputs in form input type="number"?

From Dev

Don't allow typing alphabetic characters in a <input type=number />

From Dev

Data type to store non-numeric registration number in a table

From Dev

Prevent user from typing in input at max value

From Dev

Jscript - checking for non numeric input

From Dev

Jscript - checking for non numeric input

From Dev

javascript check html input type number is not empty with non numerical values

From Dev

Android EditText input changing as we typing on it(number)

From Dev

How to limit typing number in text input

From Dev

Android EditText input changing as we typing on it(number)

From Dev

Mobile Safari - How to Default to Full Numeric Keypad for Decimal Entry w/o type="number", or disable Safari input type="number" correction

From Dev

Prevent specific number in html input?

From Dev

Prevent specific number in html input?

From Dev

Python store non numeric string as number

From Dev

HTML5 <input> type = number prevent user from violating the min="" or max="" value

From Java

Styling a input type=number

From Dev

Input type number in AngularJS

From Dev

Input type number validation

From Dev

Check Numeric vs. Non-numeric column type in MySQL

From Dev

How can I prevent a user from typing more then 4 uinits of precision in a kendo numeric textbox

From Dev

"invalid input syntax for type numeric" for entering "emptyness"

From Dev

Scala casting a number to Numeric[T] type

From Dev

binding input and non-input numeric contents for calculation of a series of values

From Dev

Non Duplicate number in user input

From Dev

HTML input number moves cursor off center when typing letters

From Dev

angular validate input type="number"

From Dev

Using ngMessage for input type number

From Dev

input type = number validation in angularjs

Related Related

  1. 1

    HTML5 input type=number value is empty in Webkit if has spaces or non-numeric characters?

  2. 2

    Prevent negative inputs in form input type="number"?

  3. 3

    Don't allow typing alphabetic characters in a <input type=number />

  4. 4

    Data type to store non-numeric registration number in a table

  5. 5

    Prevent user from typing in input at max value

  6. 6

    Jscript - checking for non numeric input

  7. 7

    Jscript - checking for non numeric input

  8. 8

    javascript check html input type number is not empty with non numerical values

  9. 9

    Android EditText input changing as we typing on it(number)

  10. 10

    How to limit typing number in text input

  11. 11

    Android EditText input changing as we typing on it(number)

  12. 12

    Mobile Safari - How to Default to Full Numeric Keypad for Decimal Entry w/o type="number", or disable Safari input type="number" correction

  13. 13

    Prevent specific number in html input?

  14. 14

    Prevent specific number in html input?

  15. 15

    Python store non numeric string as number

  16. 16

    HTML5 <input> type = number prevent user from violating the min="" or max="" value

  17. 17

    Styling a input type=number

  18. 18

    Input type number in AngularJS

  19. 19

    Input type number validation

  20. 20

    Check Numeric vs. Non-numeric column type in MySQL

  21. 21

    How can I prevent a user from typing more then 4 uinits of precision in a kendo numeric textbox

  22. 22

    "invalid input syntax for type numeric" for entering "emptyness"

  23. 23

    Scala casting a number to Numeric[T] type

  24. 24

    binding input and non-input numeric contents for calculation of a series of values

  25. 25

    Non Duplicate number in user input

  26. 26

    HTML input number moves cursor off center when typing letters

  27. 27

    angular validate input type="number"

  28. 28

    Using ngMessage for input type number

  29. 29

    input type = number validation in angularjs

HotTag

Archive