Keypress event in jquery

0

Hi, I am testing this code to allow a textbox to enter only numbers with Jquery. When I try it in Chrome it works perfect, but the problem arises when I try it from Firefox since it disables the delete key, I can not erase what I write. A help please.

Jquery code that I am using:

    $(function(){
        $('#input').keypress(function(e){
            if(e.charCode < 48 || e.charCode > 57){
                return false;
            }
        });
    });
    
asked by frank tomaylla vilca 22.10.2017 в 07:15
source

1 answer

0

There are pages that block the entry of characters, copy or paste strings, etc. In my opinion, this is a terrible user interface design and it is much better to validate the entry of information and display a message if it is not correct. There are also plugins to obtain only numeric fields. But hey, this works:

$(function(){
        $('#input').keypress(function(e){
            console.log(e.charCode);
            if ((e.charCode >= 48 && e.charCode <= 57) || (e.charCode == 0)) {
                return true;
            } else {
                return false;
            }
        });
    });
    
answered by 22.10.2017 / 15:41
source