1,js禁止输入中文
复制代码 代码示例:
<script>
function nocn(){
for(i=0;i<document.getElementsByName("nocn")[0].value.length;i++){
var c = document.getElementsByName("nocn")[0].value.substr(i,1);
var ts = escape(c);
if(ts.substring(0,2) == "%u"){
document.getElementsByName("nocn")[0].value = "";
alert("这里不能输入中文/全角字符");
}
}
}
</script>
<table bgcolor="#6699ff" align="center">
<tr>
<td>不能输入中文:</td>
<td><input type="text" name="nocn" onKeyup="nocn()" onblur="nocn();"></td>
</tr>
<tr>
<td>能输入中文:</td>
<td><input type="text" name="cnok"></td>
</tr>
</center>
2,禁止输入中文检测代码。
复制代码 代码示例:
var reg = /^([u4E00-uFA29]|[uE7C7-uE7F3])*$/;
var reg = /[^u4e00-u9fa5]/;
var reg = new RegExp("^([u4E00-uFA29]|[uE7C7-uE7F3])*$");
<script>
//var reg = /^([u4E00-uFA29]|[uE7C7-uE7F3])*$/;
var reg = /[^u4e00-u9fa5]/;
//var reg = new RegExp("^([u4E00-uFA29]|[uE7C7-uE7F3])*$");
var name = document.getElementById("userName").value;
function test(){
alert(reg.test(name));
}
</script>