views:

106

answers:

2

Hello all,

So I'm trying to find a way to have the jQuery validation plugin simply return false when the form is invalid, and not show the error messages or do anything else. But this is proving more difficult than it should be. I'm setting up the validation as such:

var validator = $journalForm.validate({
 rules: {
  EntryDate: { required: true, date: true },
  EvalStatus: { required: true },
  EvalState: { required: true },
  ActivityType: { required: true },
  Duration: { required: true },
 }
});

And I'm testing to see if the form is valid as such:

if ($form.valid()) {
    alert("success");
}
else {
    alert("fail");
}

The problem is that when it hits the if statement above, if the form is invalid, it doesn't even get to the else. Instead, it just shows the errors. However, if the form is valid, it reaches the alert and pops up "success". How can I just get a simple "false" returned from the plugin when the form doesn't pass the rules, and have it do nothing else?

Thanks.

A: 

http://docs.jquery.com/Plugins/Validation/validate#options

according to that you can pass an invalidHandler callback function to validate(). i don't know if that's before or after the messages show up, but it's something to look at.

Brandon H
A: 

You can use the showErrors option for this, passing it a function that does nothing, like this:

$journalForm.validate({
  rules: {
    EntryDate: { required: true, date: true },
    EvalStatus: { required: true },
    EvalState: { required: true },
    ActivityType: { required: true },
    Duration: { required: true },
  },
  showErrors: function() { }
});

This will prevent errors for displaying as they normally would, then you can just do this to get the status:

alert($journalForm.valid() ? "success" : "fail");
Nick Craver