不那么简洁的方法:if (s.match(/^[_0-9a-zA-Z\u4e00-\u9fa5]+$/) && !s.match(/^\d+$/)) {
//上面判断是否由汉字、英文字符、数字和下划线组成,并且不能是纯数字 var tempLength = s.length + s.replace(/[\x00-\xff]/g, "").length;
//上面一行计算长度,一个汉字的长度为2 if (tempLength >=4 && tempLength <= 40) {
//符合要求...
} else {
//不符合要求...
}
}
//上面判断是否由汉字、英文字符、数字和下划线组成,并且不能是纯数字 var tempLength = s.length + s.replace(/[\x00-\xff]/g, "").length;
//上面一行计算长度,一个汉字的长度为2 if (tempLength >=4 && tempLength <= 40) {
//符合要求...
} else {
//不符合要求...
}
}
aww="fewge你我w他"
str=aww
aa=/([\u00FF-\uFFFF])/
while(aa.test(str)){
str=str.replace(aa,"aa")
}
alert(str.length)
</script>
{
if(teststr.replace(/[\u4e00-\u9fa5]|\w/g,"") != "")return false;
if(teststr.replace(/\d/g,"")=="")return false;
if(teststr.replace( /[\u4e00-\u9fa5]/g,"aa").length >40 || teststr.replace( /[\u4e00-\u9fa5]/g,"aa").length < 4 )return false;
return true;
}