使用Jquery Validate插件删除动态验证

时间:2010-08-06 17:32:25

标签: jquery validation jquery-validate

我的问题的简化版本在这里: http://www.arianhojat.com/temp/jquery/dynamic_validate/test.html

基本上我有一个电子邮件规则,并根据复选框我删除它...但它仍然没有被删除...我尝试用“添加”将其移动到自己的规则,然后尝试删除通过它伴随着“删除”功能,但仍然存在同样的问题。

因此,在您填写您的名字后,请尝试提交。 有关电子邮件字段的错误... 然后选中复选框,它应该删除#email的验证,它只删除一半。就像你尝试使用firstname提交一样,它允许它......但现在在电子邮件字段中键入内容并尝试提交...它会报错,说它无效(但验证已被删除?)

谢谢!

代码在此供参考:

$(document).ready(function(){


  $("#contactForm").validate({  
onfocusout: false,
debug: true,

    rules: {      
      fname: {
        required: true
      }
    },

    messages: {            
      fname: {
        required: "* Please enter a first name"
      }
    },

    errorLabelContainer: "#messageBox",
    wrapper: "li",
    submitHandler: function(form) {              

      alert('Submitted');
      return false;
    }
  });

  $("#email").rules("add", {
    required: true,
    email: true,
    messages: {
        required: "* Please enter a sweet email address",
        email: "* Please enter a sweet valid email address"
    }
  });

  $('#no_email').bind('change', function(event) {

    if( $(this).attr('checked') ) {
console.log('removed?');

      $("#email").rules("remove");  //, "required email" ... still doesnt work if specify the rules to remove

    } else {

console.log('re-added?');
      $("#email").rules("add", {
        required: true,
        email: true,
        messages: {
          required: "* Please enter an awesome email address",
          email: "* Please enter an awesome valid email address"
        }
      });

    } //else 


    $("#contactForm").validate().element( "#email" );



  });


});
....

<form id="contactForm" action="doesnt_exist_yet2.html" method="post"> 

  <ul id="messageBox"></ul> 

  <div style="">
    <p>First name:</p>
    <input type="text" name="fname" id="fname" class="fname" maxlength="100"/>
  </div>


  <div style="">
    <p>Your Email Address</p>
    <input type="text" name="email" id="email" class="email" maxlength="100"/>
  </div>


  <br/><br/>
  <input type="checkbox" name="no_email" id="no_email" /> Turn off validation for EMAIL




  <input type="submit" name="submit_btn" id="submit_btn" value="Submit">
</form>

1 个答案:

答案 0 :(得分:1)

我想我解决了它,不能拥有#email和电子邮件规则。我将我的字段更改为#myemail并认为它现在有效。 errrrr!