Jquery: filter input on keypress

前端 未结 5 756
花落未央
花落未央 2020-12-18 04:45

I have a text field, which will accept only the following characters:

Allowed characters: [a-z 0-9 + # - .]

This is the same filter SO

5条回答
  •  盖世英雄少女心
    2020-12-18 04:47

    This worked for me:

    $(function(){
        $('#t').keypress(function(e){
            var txt = String.fromCharCode(e.which);
            console.log(txt + ' : ' + e.which);
            if(!txt.match(/[A-Za-z0-9+#.]/)) 
            {
                return false;
            }
        });
    });
    
    
    

提交回复
热议问题