例子如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>禁止输入中文</title> <script src="../lib/jquery.min.js"></script> </head> <body> <h1>禁止输入中文</h1> 方法二:onchange事件 :在松开按键时用脚本检查文本框的值,只保留Unicode编码在0和255之间的字符。 (IOS和安卓已测试通过) <script> function check(str){ var temp="" for(var i=0;i<str.length;i++) if(str.charCodeAt(i)>0&&str.charCodeAt(i)<255) temp+=str.charAt(i) return temp } </script> <input onpaste="return false" id="txt_card1" ondragenter="return false" onchange="this.value=check(this.value)"> <br> 方法三:把所有双字节字符替换为空。 <input onpaste="return false" id="txt_card2" ondragenter="return false" > <br> 方法四:把中文字符替换为空。 <input onpaste="return false" id="txt_card3" ondragenter="return false" onkeyup="this.value=this.value.replace(/[u0391-uFFE5]/gi,'')"> <input type="button" onclick="NoInputInChinese();" value="验证是否包含中文" /> <h1>禁止输入数字</h1> <script type="text/javascript"> function NoInputInChinese(obj) { var str = obj.value; var temp = "" for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 255) temp += str.charAt(i); } obj.value=temp; } //工具方法:禁止输入数字 function noNumbers(e, obj) { /// <summary>禁止输入数字</summary> /// <param name="e" type="String">事件</param> /// <param name="e" type="Object">当前元素</param> var keynum var keychar var numcheck if (window.event) // IE { keynum = e.keyCode } else if (e.which) // Netscape/Firefox/Opera { keynum = e.which } keychar = String.fromCharCode(keynum) if ((keynum >= 48 && keynum <= 57) || (keynum >= 96 && keynum <= 105)) { switch(keynum) { case 96: keychar="0"; break; case 97: keychar="1"; break; case 98: keychar="2"; break; case 99: keychar="3"; break; case 100: keychar="4"; break; case 101: keychar="5"; break; case 102: keychar="6"; break; case 103: keychar="7"; break; case 104: keychar="8"; break; case 105: keychar="9"; break; } obj.value = obj.value.replace(keychar, ''); } //keychar = String.fromCharCode(keynum) //numcheck = /d/ //var flag = numcheck.test(keychar); //if (flag) { // obj.value = obj.value.replace(keychar, ''); //} } function checkdd(){ alert($("#txtNation").val()); var r =$("#txtNation").val().match("\d"); if (r == null) { alert("无匹配数字"); }else { alert("已包含数字"); } } </script> <input type="text" id="txtNation" placeholder="您的民族" onkeyup="noNumbers(event,this);" /> <input type="button" onclick="checkdd();" value="测试" /> </body> </html>
代码: