How to ignore unwanted characters from a text field (JavaScript or jQuery)

There is a TextBox that I want to allow users to simply enter numbers, not any alphabetical characters. So, how could I ignore those characters entered by the end user via JavaScript or jQuery? Note that I do not want to replace the user-entered value with an empty string; Instead, you want to ignore the case if there is any way to do this.

+3
source share
5 answers

try this code:

  $("#id").keypress(function (e)
     {
     //if the letter is not digit then display error and don't type anything
     if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
     {
     return false;
     }
     });

link http://roshanbh.com.np/2008/04/textbox-accept-only-numbers-digits.html "

+5
source

, , , ? ?

JS, string.toLowerCase()

0

keypress. event.which, , , ( 48 57), false, .

$("input").keypress(function (e) {
    if (e.which < 48 || e.which > 57)
        return false;
});
0

Source: https://habr.com/ru/post/1780425/


All Articles