<html>
<head><script language="javascript">function chkvalue(txt)
{
   if(txt.value=="")
   {
     alert("这里不能为空!");
     document.form1.txt.onfocus();
   }
   else
   {
      if(!check(txt))  alert("请按正确的格式输入");
   }
}
  function check(str)
  {
     return str.match(\^[A-Za-z0-9_]{6,16}$\);
  }
</script>
</head><body><form name="form1" action="" method="get" onsubmit="display()" target="_self">
姓   名:<input type="text" value="" name="name1" id="name1" onBlur="chkvalue(this)" /><br />
</form></body>
</html> 

解决方案 »

  1.   

    <html>
    <head><script language="javascript">function chkvalue(txt)
    {
      if(txt.value=="")
      {
      alert("这里不能为空!");
      txt.focus();
      }
      else
      {
      if(!check(txt.value)) alert("请按正确的格式输入");
      }
    }
      function check(str)
      {
      return str.match(/^[A-Za-z0-9_]{6,16}$/);
      }
    </script>
    </head><body><form name="form1" action="" method="get" onsubmit="display()" target="_self">
    姓 名:<input type="text" value="" name="name1" id="name1" onBlur="chkvalue(this)" /><br />
    </form></body>
    </html>