views:

946

answers:

5

I'm using the Jquery Validation plugin on a project.

By default, the plugin validates input when the submit button is clicked. The behavior is "lazy" in order to be unobtrusive to the user. If an error is found, the validation then becomes "eager" and validates input as the user corrects the offending entries.

Is there any way to override the initial "lazy" behavior through an option? I can't find any solution in the documentation.

Example:

If you look at this demo, you will notice that the user can navigate through the form leaving empty text boxes that will fail validation when the user clicks on submit. If the user initially enters an invalid email address on the email textbox, it is not highlighted until the submit button is clicked. After that, the input is validated on each key press and blur event. That is what I'd like to activate from the start.

I understand the design choice of the plugin creator, but I need different behavior.

+4  A: 

Actually, the default behavior I get is validation on the focusout and keyup events, which I see in the plugin code as defaults. I have to disable those in the plugin configuration in order to only get validation at submit. Perhaps you should post some of your code... I don't think the situation is completely clear.

chaos
Thank you for your prompt reply. I've expanded my question to clarify.
robDean
Thanks. I don't get the same behavior as you get from that demo. Errors are displayed for me as I navigate through the form. Possibly you should include your browser and version in the question and try some other ones.
chaos
Hmm, though I do see that if I leave fields blank they don't validate until submit. It's if I enter something invalid (including an invalid email), or blank it out after an error is displayed, that it works.
chaos
I think you can force the behavior you want by adding onfocusout: function() { $(this).valid() } to your validate() setup call.
chaos
Except with the semicolon where it should be. Like $('#myform').validate({ onfocusout: { $($this).valid(); } };
chaos
Thanks! This seems to do the trick:$('#myform').validate({ onfocusout: function(element) { $(element).valid(); } };
robDean
+2  A: 

Thanks to chaos for guidance on this question:

To enable eager validation on jquery validation plugin, add the following option to the validate function:

$('#myform').validate({ onfocusout: function(element) { $(element).valid(); } };

robDean
A: 

Nice one, this works!

A: 

Exactly what I was looking for. Thanks a lot.

Does anyone have any idea how to implement the same eager validation for radio buttons?

Paolo Futre
I'm looking for this one too
atika
A: 

This is perfect, saved me quite some time, thanks a bunch!! :)

pouipouidesign