21

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.

1
  • The plugin contains the remote method specifically for this task.
    – Sparky
    Oct 14, 2014 at 22:00

4 Answers 4

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

4
  • 9
    A quick note to those who do not click the link. You have to call $("#myform").validate(); first.
    – PFranchise
    Jan 18, 2013 at 17:51
  • 2
    Note that this code doesn't run at all if the user inputs the name via, say, right click, Paste. Use remote. Oct 19, 2014 at 3:02
  • Use focusout instead of keyup. And $("#myform").validate() is not required for manual validation.
    – Ed DeGagne
    Jan 15, 2015 at 22:28
  • I think that's wrong. the valid() delegates the check to the form of the element, so if another element of the form is invalid but the one you check is valid, the answer will still be 'true'
    – Chris
    Sep 30, 2016 at 16:18
8

Validator.element()

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

http://jqueryvalidation.org/Validator.element

2
  • That is to trigger validation. The OP asked how to check if it is valid Aug 9, 2018 at 11:48
  • 1
    @user2903379 read the docs: Validates a single element, returns true if it is valid, false otherwise. You see what I did there? RETURNS TRUE IF VALID
    – Mark Homer
    Sep 11, 2018 at 9:06
2

This worked for me on JQuery 1.11

$("#userName").keyup(function () {
    if ($(this)[0].validity.valid) {
        // AJAX here
    }
});
0

Hello i find exclusive solution, i have the same problem as 47k others & i discover the solution without other jquery plugins or libraries: -first of all we need to register our input in a variable, -then on change checkValidity() if true:mean if valid we assign the registered var to our input to rest it for next change.

        ivar=$('input[name="y-i-name"]')[0];
        $('input[name="y-i-name"]').on('change',function(){
             if($('input[name="y-i-name"]')[0].checkValidity()){


                     // your custom code here!



                     $('input[name="y-i-name"]')[0]=ivar; // & now we rest the input for next change validation
             }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.