only allow English characters and numbers for text input

前端 未结 7 748
旧巷少年郎
旧巷少年郎 2020-12-05 03:42

Live Demo: http://jsfiddle.net/thisizmonster/DveuB/

How can I change this so that the input only allows the characters A-Z, a-z, 0-9 while typing, without using a re

7条回答
  •  星月不相逢
    2020-12-05 04:12

    I'm using this function.

    By modifying RegExp you can get rid of any characters you don't like personally.

    $(function(){
        $("#user").bind('keypress',function(e){
            var regex = new RegExp("^[a-zA-Z0-9 ]+$");
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) return true;
            e.preventDefault();
            return false;
        });
    });
    

提交回复
热议问题