0

Not entirely sure and cannot find a setting to allow 1 of x number of validations to pass validation on an entire form.

I've had to use the .rules("add" function as my fields have array keys in the name attribute. jQuery Validate works as expected when all fields fail but if 1 field has valid content, the whole form passes validation.

<form id="new_user_session">
   <input class="input-block-level sq-input" id="user_session_email" name="user_session[email]" placeholder="email address" type="text">
   <input class="input-block-level sq-input" id="user_session_password" name="user_session[password]" placeholder="password" type="password">
</form>

<script type="text/javascript">
  jQuery(function() {
      jQuery('#new_user_session').validate();

      jQuery("#user_session_email").rules("add", {
          email: true,
          messages: {
              email: "Please insert a valid email address",
              required: "Please insert your email address"
          },
          required: true
      });

      jQuery("#user_session_password").rules("add", {
          messages: {
              required: "Please insert your password"
          },
          required: true
      });
  });
</script>

Thanks in advance !

Dallas Clark
  • 4,064
  • 3
  • 30
  • 36

2 Answers2

1

After some searching, it seems to be a known issue with the last version of the validate plugin. It happens when you use the messages property when adding a rule.

You have two immediate solutions to this problem:

EDIT: The author fixed the problem with this commit, so you can add a third solution:

Andreas Schwarz
  • 1,788
  • 17
  • 42
1

You do not need to use the rules('add') method in the first place, so you can easily avoid the bug entirely.

Simply follow the documented guidelines for names that contain brackets and surround them in quotes.

Working DEMO: http://jsfiddle.net/K65e5/

$(document).ready(function () {

    $('#new_user_session').validate({
        rules: {
            'user_session[email]': {
                email: true,
                required: true
            },
            'user_session[password]': {
                required: true
            }
        },
        messages: {
            'user_session[email]': {
                email: "Please insert a valid email address",
                required: "Please insert your email address"
            },
            'user_session[password]': {
                required: "Please insert your password"
            }
        }
    });

});
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • 1
    I thought I tried quotes around the names of the fields and it failed. But, tried it again and it worked as expected. Thank you very much! – Dallas Clark Mar 20 '13 at 20:59