用javascript实现input文本框只能输入中文的方法:
<script type="text/javascript">
function ischarsinbag (s, bag)
{
var i,c;
for (i = 0; i < s.length; i++){
c = s.charAt(i);//字符串s中的字符
if (bag.indexOf(c) > -1){
return c;
}
}
return "";
}
//检查函数
function ischinese(s)
{
var errorchar;
var badchar = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789><,[]{}?/+=|//":;~!#$%()`";
errorchar = ischarsinbag( s, badchar)
if (errorchar != "" ){
report="请重新输入中文/n";
alert(report);
return false;
}
return true;
}
</script>
<input name="test" id="test" onblur="ischinese(this.value);" type="text"/>
原文地址:http://www.corange.cn/archives/2009/12/3506.html
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)