Dallas Clark
Dallas Clark

Reputation: 4112

jQuery Validate passes with only 1 field passing validation

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 !

Upvotes: 0

Views: 944

Answers (2)

Sparky
Sparky

Reputation: 98728

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"
            }
        }
    });

});

Upvotes: 1

Andreas Schwarz
Andreas Schwarz

Reputation: 1798

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:

Upvotes: 1

Related Questions