Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifiy the order of the validators in jQuery validate plugin

I'm wondering if it's possible to specify the order in which the validators are run.

Currently I wrote a custom validator that check if it's [a-zA-Z0-9]+ to make sure the login validates our rules and a remote validator to make sure that the login is available but currently the remote validator is lauched before my custom validator. I would like to launch the remote validator only if the element validates my custom validator.

Any clue ?

thanks for reading

like image 426
Paté Avatar asked Apr 15 '11 16:04

Paté


People also ask

How do you check if jQuery validate is working?

fn , so simply check whether it exists or not; if (typeof jQuery. fn. validationPlugin === "function") { // It's loaded } else { // It's not. }

What is validate in jQuery?

Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.

What is JavaScript validation plugin?

jQuery Validation is a javascript based plugin that helps you implement a powerful client side validator to your form elements that validate the value ( name, email, address, etc..) your user input when submitting.

What is Errorplacement?

Definition of jQuery validate errorplacement. The jQuery validate errorplacement() function is used to customize the placement of an error message. This function is a built-in function in jQuery. These function changes the default behavior of the validation plugin to insert the error label after the input fields.


1 Answers

You need to augment/monkey patch/overwrite (whichever term makes the most sense to you) the $.fn.rules that the validate plugin uses when creating the rules to run for a field to push your rule to second place in the rules object, after the required rule.

If you search for rules in the script, you'll need to add similar logic to this just before if (data.required)

If (data.yourRuleName) {
    var param = data.yourRuleName;
    delete data.yourRuleName;
    data = $.extend({ yourRuleName: param }, data);
}

Or you can copy the rules section out, add the above and put it in a script that is referenced after the validate script. This way, if the script changes, you won't have to go applying these changes to the new version of the script (assuming that rules would still work the same way).

like image 94
Russ Cam Avatar answered Oct 09 '22 12:10

Russ Cam