1

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

steve
  • 471
  • 6
  • 15

1 Answers1

5

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

});
Jake Aitchison
  • 1,079
  • 6
  • 20
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Nov 06 '17 at 13:34