Best way to restrict a text field to numbers only?

前端 未结 29 1682
长情又很酷
长情又很酷 2020-12-04 22:01

I\'m using the following Javascript to restrict a text field on my website to only accept numerical input, and no other letters or characters. The problem is, it REALLY reje

29条回答
  •  长情又很酷
    2020-12-04 22:24

    this will enable the numpad inputs also.

    .keydown(function(event){                                     
        if(event.keyCode == 8 || event.keyCode == 46)             
            return true;                                         
        if(event.keyCode >= 96 && event.keyCode <= 105)           
            return true;                                          
        if(isNaN(parseInt(String.fromCharCode(event.keyCode),10)))
           return false;                                          
    }); 
    

提交回复
热议问题