var pattern = /[^\w\u4E00-\u9FA5-]/g;
var str = "hfa!f不";
if (pattern.test(str)) {
msg = "用户名包含非法字符";
}\w 匹配 a-z A-Z 0-9 和_(下划线)

解决方案 »

  1.   

    <script language="javascript">
    function IsDigit(cCheck) { return (('0'<=cCheck) && (cCheck<='9')); }
    // 判断是否是0到9之间的数字
    function IsAlpha(cCheck) { return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) }
    //判断是否是a到z或A到Z之间的字母
    function chk()
    {
    if (form1.aaaid.value=="")
    {
    alert("编号不能为空.");
    form1.aaaid.focus();
    return false;}
    for (nIndex=0; nIndex<document.form1.aaaid.value.length; nIndex++)
        {
            cCheck = document.form1.aaaid.value.charAt(nIndex);
          
            if (!(IsDigit(cCheck)))
            {
                alert("编号不正确!");
                document.form1.aaaid.focus();
                return false;
            }
          }
    return true
    }
    </script>