<form name="form1" method="post" action="register.jsp"  onSubmit="return check()">中返回
check()函数,check()在脚本定义:
<script>
function check(){
  username=document.form1.username.value;
  pwd1=document.form1.pwd1.value;
  pwd2=document.form1.pwd2.value;
  year=document.form1.year.value;
  month=document.form1.month.vlaue;
  day=document.form1.day.value;
  email=document.form1.email.value;
  interest=document.form1.interest.value;
  
  if(username.length<6||username.length>15){
      alert("用户名长度必须在6-15位之间!");
  return false;
   }
   if(pwd1.length<6||pwd1.length>20){
      alert("密码长度必须在6-20位之间!");
  return false;
}
if(pwd1!=pwd2){
   alert("两次输入的密码不匹配!");
   return false;
 }
 if(year.length!=4||month>13||month<1||day>32||day<1){
     alert("生日输入不正确!");
 return false;
  }
  if(email=""||(email.indexOf('@')==-1||(email.indexOf('.')==-1)){
     alert("电子邮件格式不正确!");
 return false;
   }
       return true;
 }
</script>
为什么每次输入不正确的信息,比如:两次输入的密码不同,不弹出alert()的内容呢?请大家帮帮忙吧!!!

解决方案 »

  1.   

    你试试把每个变量先初始化为"",如var username="",再赋值;如果还是不行,那就用最恶心的办法,不要通过变量判断,直接判断如下:
    if(document.form1.username.value.length<6||document.form1.username.value.length>15){
          alert("用户名长度必须在6-15位之间!");
      return false;
       }
      

  2.   

    你的方法没有问题,我试过了,有些小毛病
    比如:month=document.form1.month.vlaue;中value写错了还有if(email=""||(email.indexOf('@')==-1||(email.indexOf('.')==-1))这句话,括号不匹配
      

  3.   

    document.form1.pwd1.value
    document.all.form1.pwd1.value
      

  4.   

    如果是在form里的onsubmit中验证的话,当验证代码出错时就会提交,所以最好应该在提交按钮的onclick事件上加,如果都没错的话再form.submit()。