汉字问题
我有个表单,现在要求用户输入真实姓名,我想用个JavaScript判断下他输入的是不是汉字,并且汉字为2-4个???
------解决方案-------------------- <input type= "text " name= "text1 " id= "text1 ">
<input type= "button " value= "ClickMe " onclick= "isChinese(text1.value) ">
<script language= "javascript ">
function isChinese(s)
{
var patrn=/^[\u0391-\uFFE5]{2,4}$/; //正规表达式的判断是否是中文
if (patrn.test(s))
alert( "OK! "); //如果满足条件的操作
else
alert( "ERROR! ");//不满足条件时的操作
}
</script>
------解决方案--------------------楼上正解!
------解决方案--------------------完整的例子:
<html>
<head>
<script language= "javascript ">
<!--
function CheckStr(str)
{
if(str > = "~ ")
{
return true;
}
else
{
return false;
}
}
function window_onload()
{
//var str = "t ";
var str = "汉 ";
alert(CheckStr(str));
}
-->
</script>
</head>
<body onload= "window_onload() ">
</body>
</html>