2013-05-06 49 views
0

我的JS验证代码有问题。当我提交表格并且有错误时,表格不应该再进一步。但是,代码并没有停止,而是继续到下一行,虽然仍然存在错误,但它显示出成功的消息。Javascript验证没有按照规定执行

而且我已经写清楚,如果,例如该字段为空,然后return false ...

为什么到下一行代码随身携带,即使有return false

submit你会明白我的意思。

JS:

(function(window, $) { 

    var Namespace = (function(Namespace) { 

     Namespace = { 

      // Main 
      run : function() { 
       this.validate.run('form'); 
      },    

      // Validation 
      validate : { 

       // error message span 
       messageBox : '<span class="message" />', 

       // add any field here 
       fields : { 
        nameField : $('#contact-name'), 
        emailField : $('#contact-email'), 
        phoneField : $('#contact-phone') 
       }, 

       // run validation 
       run : function(formName) { 

        $(formName).on('submit', $.proxy(this.validateField, this)); 
       }, 

       validateField : function() { 
        for (var field in this.fields) { 
         if (this.fields.hasOwnProperty(field)) { 
          this.checkField(this.fields[field]); 
         } 
        } 

        $('#general-message-section').text('Form successfully sent, thank you!'); 
        return false; 
       }, 

       checkField : function(field) { 

        var messageBox = $(this.messageBox); 

        field.closest('li').find('.message').remove(); 

        if (field.hasClass('required')) { 
         if (!field.val()) { 
          messageBox.text('This field is empty!'); 
          field.closest('li').append(messageBox); 
          return false; 
         } 
        } 

        if (this.fields.emailField.val()) { 
         this.fields.emailField.closest('li').find('.message').remove(); 

         if (!this.fields.emailField.val().match(this.regEx.email)) { 
          messageBox.text('Only email format accepted!'); 
          this.fields.emailField.closest('li').append(messageBox); 
          return false; 
         } 
        } 

        if (this.fields.phoneField.val()) { 
         this.fields.phoneField.closest('li').find('.message').remove(); 

         if (!this.fields.phoneField.val().match(this.regEx.numbers)) { 
          messageBox.text('Only numbers are accepted!'); 
          this.fields.phoneField.closest('li').append(messageBox); 
          return false; 
         } 
        } 
       }, 

       regEx : { 
        email : /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/, 
        numbers : /^[0-9]+$/ 
       } 
      } 
     }; 

     return Namespace; 

    }(Namespace || {})); 

    // make global 
    window.Namespace = Namespace; 

}(window, jQuery)); 

// run it... 
Namespace.run(); 

HTML:

<p id="general-message-section"></p> 
<form id="contact-form" method="post" action="#"> 
    <fieldset> 

     <ul> 
      <li> 
       <label for="contact-name">Contact name *:</label> 
       <input type="text" id="contact-name" tabindex="1" class="required" autofocus /> 
      </li> 
      <li> 
       <label for="contact-email">Contact email address *:</label> 
       <input type="text" id="contact-email" tabindex="2" class="required" /> 
      </li> 
      <li> 
       <label for="contact-phone">Contact phone number:</label> 
       <input type="text" id="contact-phone" tabindex="3" /> 
      </li> 
      <li> 
       <input type="submit" id="submit-btn" tabindex="4" value="Submit" /> 
      </li> 
     </ul> 
    </fieldset> 
</form> 

非常感谢

+1

如果您在问题本身中发布相关代码,它更容易(对于其他人)! – techfoobar 2013-05-06 16:40:49

+1

作为将来参考的一个要点,请在您的问题中包含代码的复制粘贴。 – ericosg 2013-05-06 16:40:50

+2

你的回报错误是在你的消息之后,所以他们自然会按顺序执行。另一个函数内的返回不会破坏父函数。你也必须检查内部函数的结果,如果它是假的,就返回它。 – ericosg 2013-05-06 16:42:40

回答

1

这将检查所有必填字段,并设置valid为false,如果任何checkField()返回false,但不会打破For循环,它会检查是否有效是在循环后假,并打破:

 validateField : function() { 
      var valid = true; 
      for (var field in this.fields) { 
       if (this.fields.hasOwnProperty(field)) { 
        if(this.checkField(this.fields[field]) === false) valid = false; 
       } 
      } 
      if(!valid) return false; 

      $('#general-message-section').text('Form successfully sent, thank you!'); 
     } 

jsfiddle

2

我猜你缺少你的验证逻辑检查。您的代码:

validateField : function() { 
    for (var field in this.fields) { 
     if (this.fields.hasOwnProperty(field)) { 
      this.checkField(this.fields[field]); 
     } 
    } 

    $('#general-message-section').text('Form successfully sent, thank you!'); 
    return false; 
}, 

不会调用checkField,但不检查其结果(可以是假的)。我猜你会碰到这样的:

validateField : function() { 
    for (var field in this.fields) { 
     if (this.fields.hasOwnProperty(field)) { 
      if(!this.checkField(this.fields[field])) { 
       alert("There are errors!"); 
       return false; 
      } 
     } 
    } 

    $('#general-message-section').text('Form successfully sent, thank you!'); 
    return false; 
}, 

当然return true;checkField,如果它是正确的(末),否则将不能工作。

+0

感谢@Meta,但只有1个字段正在显示一个错误。我希望每个字段同时在旁边显示一条错误消息,因为它们全都是空的。我怎样才能做到这一点? – Shaoz 2013-05-06 17:06:54

+0

您可以在您的'validateField'函数中将布尔值初始化为'true',然后每当对'checkField'的调用为false时将其设置为'false'。循环检查布尔值并显示您需要的消息(错误或成功)。为了工作,如果一切正常,你需要'checkField'返回'true'。希望能帮助到你! – Meta 2013-05-06 17:18:48

+0

你不需要在checkField中返回true,你可以改为检查checkField是否等于fasle,检查我的答案。 – razzak 2013-05-06 17:25:02