<html>
<head>
<title>字符串格式检验</title>
<script>
function checkStrFormat(str)
{
if(/^([a-zA-Z_]{1})([\w]*)$/g.test(this.value))
{
alert("just letter is permitted"); // 报警提示语句。
}
}
</script>
</head>
<body>
<form name="timeForm" action="resultOK.html">
<input type="text" onblur="checkStrFormat(this.value)">
  </form>
</body>
</html>
代码如上图所示。
代码需要限制字符串只能以字母开头,内容可以包含数字,字母,下划线。
结果现在是输入什么内容都会执行alert语句。
不知道问题出在哪里。请指教。

解决方案 »

  1.   

    if(/^([a-zA-Z_]{1})([\w]*)$/g.test(this.value))应该是if(/^([a-zA-Z_]{1})([\w]*)$/g.test(str))
      

  2.   

    function checkStrFormat(str){
    if(/^[a-zA-Z_]([\w_]*)$/g.test(str)){
    alert("ok,..."); // 报警提示语句。
    }else{
    alert('wrong...');
    }
    }var x='a1234';   //ok
    //var x='5a1234';   //wrong
    checkStrFormat(x);
      

  3.   

    改过以后,和我想要的结果是相反的。输入123,没有问题。
    abc123,马上报错。
      

  4.   

    你那个提示信息。。它对的就会输出来了。。不对当然没提示了。。因为你没写else啊。。