A little bit of context:
I'm using the jQuery Validation plugin to validate a sign-up form. I now want to implement an ajax call to check whether the user name is available in the system, and I want to make this ajax call only if the userName value is a valid one as per the rules set in $(form).validate();
I want something like:
$("#userName").keyup(function () {
if ($("#userName").isValid()) {
//make ajax called
}
});
I searched the documentation but i couldn't identify the solution to my problem.
The plugin adds a validationPlugin function to jQuery. fn , so simply check whether it exists or not; if (typeof jQuery. fn.
Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
Just use: $("input:empty"). length == 0; If it's zero, none are empty.
$("#userName").keyup(function () {
if ($("#userName").valid() == true ) {
//make ajax called
}
});
http://docs.jquery.com/Plugins/Validation/valid
Note: To those who do not click the link. You have to call $("#myform").validate();
first.
Validator.element()
Description: Validates a single element, returns true if it is valid, false otherwise.
http://jqueryvalidation.org/Validator.element
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With