steve
steve

Reputation: 473

Jquery Validate not always returns true

I have a button that triggers the jquery validation plugin. The problem is that it always returns true, unless the fields are empty. It ignores the rules that are parsed.

DEMO http://jsfiddle.net/sw87W/835/

$(document).ready(function() {
  $('#myForm').validate({
    email: {
      required: true,
      email: true
    },
    password: {
      required: true,
      minlength: 5
    }
  });

  $('#validateForm').click(function() {
    var isValid = $("#myForm").valid();
    alert(isValid);
  });
});

In fact you can delete my validate() function and the same behaviour is present.

DEMO http://jsfiddle.net/sw87W/836/

$(document).ready(function() {
  $('#validateForm').click(function() {
    var isValid = $("#myForm").valid();
    alert(isValid);
  });
});

This question has been asked before, but doesn't answer my question:

jquery validate - remote always returns true

jQuery Validation always returns true

Upvotes: 1

Views: 243

Answers (1)

Jake Aitchison
Jake Aitchison

Reputation: 1099

It worked when you add the rules sub object:

jquery validate documentation

JSFIDDLE

$(document).ready(function() {
  $('#myForm').validate({
  rules:{
      email: {
        required: true,
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    }
  });

  $('#validateForm').click(function() {
    var isValid = $("#myForm").valid();
    alert(isValid);
  });

});

Upvotes: 5

Related Questions