Jquery filter text boxes (Preventing user from inserting certain characters)

后端 未结 3 694
余生分开走
余生分开走 2021-01-23 16:30

I am trying to prevent users from typing certain characters in text boxes and this is the code that I have so far :

$().ready(function() {
  $(\".textbox\").keyp         


        
3条回答
  •  清酒与你
    2021-01-23 17:14

    You have to include keyVal = 48 and keyVal = 57 or you will be allowing the 0 and 9 respectively. The final code would look like this:

    $().ready(function(){
        $(".textbox").keypress(function(event){
            var keyVal =  (event.charCode || event.keyCode || event.which);
            if((keyVal >= 48 && keyVal <= 57))// Numbers
            {
                return false;
            }
        });
    });
    

    Also looks clearer using the || operator as Devon suggested.

提交回复
热议问题