Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validation plugin: how to check if an element is valid?

Tags:

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.

like image 381
Dan Burzo Avatar asked Jan 15 '09 16:01

Dan Burzo


People also ask

How do you check if jQuery validate is working?

The plugin adds a validationPlugin function to jQuery. fn , so simply check whether it exists or not; if (typeof jQuery. fn.

How do you check if a field is valid or not?

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 .

Does jQuery validate require a form?

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.

How do you check if all inputs are filled jQuery?

Just use: $("input:empty"). length == 0; If it's zero, none are empty.


2 Answers

$("#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.

like image 144
Fatih Hayrioğlu Avatar answered Sep 28 '22 07:09

Fatih Hayrioğlu


Validator.element()

Description: Validates a single element, returns true if it is valid, false otherwise.

http://jqueryvalidation.org/Validator.element

like image 42
Mark Homer Avatar answered Sep 28 '22 08:09

Mark Homer