日期:2014-05-16  浏览次数:20388 次

javascript 如何判断字符类型,要判断的类型如下:
'用途:判断字符串类型
'返回0-空
'返回1-数字
'返回2-英文
'返回3-汉字
'返回4-英汉
'返回5-英数
'返回6-汉数
'返回7-全

------解决方案--------------------
不过这里没有考虑到特殊字符和符合等问题
<script language=javascript>
function test(s)
{
if(s== " ")
{
return "0 ";
}
if(/^\d+$/.test(s))
{
return "1 "
}
if(/^[a-z]+$/i.test(s))
{
return "2 "
}
if(/^[\u4e00-\u9fa5]+$/.test(s))
{
return "3 "
}
if(/^[a-z\u4e00-\u9fa5]+$/i.test(s))
{
return "4 "
}
if(/^[a-z0-9]+$/i.test(s))
{
return "5 "
}
if(/^[\u4e00-\u9fa50-9]+$/.test(s))
{
return "6 "
}
if(/^[\u4e00-\u9fa50-9a-z]+$/i.test(s))
{
return "7 "
}
}
</script>

<input type=text onblur= "alert(test(this.value)) ">
------解决方案--------------------
<script language= "JavaScript ">
function _0009(str)
{
/^(?:(\d+)|([a-z]+)|([\u4e00-\u9fa5]+)|([\u4e00-\u9fa5a-z]+)|([a-z\d]+)|([\u4e00-\u9fa5\d]+)|([\w\W]+))$/i.exec(str);
if (RegExp.$1 != " ") return 1;
else if (RegExp.$2 != " ") return 2;
else if (RegExp.$3 != " ") return 3;
else if (RegExp.$4 != " ") return 4;
else if (RegExp.$5 != " ") return 5;
else if (RegExp.$6 != " ") return 6;
else if (RegExp.$7 != " ") return 7;
else return 0;
}
var arr = [ " ", "123 ", "abc ", "一二三 ", "abc一二三 ", "abc123 ", "一二三123 ", "abc一二三123 "];
for(var i in arr)
{
alert(arr[i] + " -> " + _0009(arr[i]));
}
</script>