2011-11-02 38 views
3

第一:我是JavaScript的新手。 所以..我有密码,重复密码,电子邮件和重复电子邮件字段的基本形式。我想检查密码是否等于重复密码。如果不是,则会显示警告消息并重新加载页面。相同的电子邮件和重复电子邮件 但是如果传递和重复密码不相等,并且电子邮件和重复电子邮件不相等,则会出现第一条警告消息,则第二条消息(此时用于电子邮件)显得过快。当两个字段不匹配时,我只想显示一条警报消息。单个else子句用于多个if子句 - javascript

<script type="text/javascript"> 

      function checkFields() { 
      var pass= document.getElementById('password'); 
      var reppass= document.getElementById('reppass'); 

      var email= document.getElementById('email'); 
      var repemail= document.getElementById('repemail'); 

      if (pass.value != reppass.value) { 
       alert('Passwords dont match'); 
       window.location.reload(); 
      } 

      if (email.value != repemail.value) { 
       alert('Emails dont match'); 
       window.location.reload(); 
      }     

      else if (pass.value != reppass.value && email.value != repemail.value) { 
       alert('Both fields dont match'); 
       window.location.reload(); 
      } 

     } 

    </script> 

而且形式:

<form onSubmit="checkFields()"> 
<p><label>Password:</label> <input name="password" id="password" required="true" type="password" /></p> 
<p><label>Repeat password:</label> <input name="reppass" id="reppass" required="true" type="password" /></p> 
<p><label>Email:</label> <input name="email" id="email" required="true" type="email" /></p> 
<p><label>Repeat Email:</label> <input name="repemail" id="repemail" required="true" type="email" /></p> 
<p><input type="submit" value="Send"></p> 
</form> 

回答

1

你可以简单地从返回如果条款是这样的:

function checkFields() { 
    var pass = document.getElementById('password'); 
    var reppass = document.getElementById('reppass'); 

    var email = document.getElementById('email'); 
    var repemail = document.getElementById('repemail'); 

    if (pass.value != reppass.value && email.value != repemail.value) { 
     alert('Both fields dont match'); 
     window.location.reload(); 
    } 

    if (pass.value != reppass.value) { 
     alert('Passwords dont match'); 
     window.location.reload(); 
     return; 
    } 

    if (email.value != repemail.value) { 
     alert('Emails dont match'); 
     window.location.reload(); 
     return; 
    } 

} 

我喜欢这种风格,因为它if引导的从句防止嵌套。缺点是,你有多个可能令人困惑的返回点 - 这很大程度上取决于函数的长度。

EDIT

,如果块

+0

这个答案的问题是,第三if语句将永远不会计算为true,因为你已经检查的值,然后返回。 – jacobsimeon

+0

第三个陈述不应该先来吗?这样,如果发现两者不匹配,它会返回? – Simon

+0

您正确更新了订单。 – topek

0
if ... 
else if ... 
else if ... 
... 
else ... 

这就是它应该如何构建。只要你喜欢,你可以有多少else if

1
if(condition1) { 
}else if(condition2) { 
}else{ 
    … 
} 

我相信这是你想要的。

1

一个解决办法是打破验证成单独的方法,那么只有当第一个成功运行第二确认更新顺序。

下面是一个例子:

var FormValiditor = function() { 
    var pass = document.getElementById('password'); 
    var reppass = document.getElementById('reppass'); 
    var email = document.getElementById('email'); 
    var repemail = document.getElementById('repemail'); 

    return { 
     checkFields: function() { 
      if(checkPassword()){ 
      return checkEmail(); 
      } 
      return false; 
     }, 
     checkPassword: function() { 
      if (pass.value != reppass.value) { 
       alert("Password don't match"); 
       return false; 
      } 
      return true; 
     }, 
     checkEmail: function() { 
      if(email.value != repemail.value){ 
       alert("Emails do not match"); 
       return false 
      } 
      return true 
     } 
    } 
}(); 

然后,如果你正在使用jQuery(你应该!)当表单被提交,你可以运行验证。

$('form').submit(FormValidator.checkFields);