ryan
ryan

Reputation: 981

jQuery Validate Plugin - Trigger validation of single field

I've got a form that can optionally be pre-populated via facebook connect. Once a user connects, their name and email are automatically filled in. The problem is that this doesn't trigger the remote validation to check if the email already exists.

Is there a way I could call the validation on that field alone? Something like:

$('#email-field-only').validate()

would be idea. Searched through the docs with no luck.

Upvotes: 98

Views: 121009

Answers (9)

Joseph Rukeijakare
Joseph Rukeijakare

Reputation: 1

I had almost the same issue but for my case, I needed to validate just 4 fields. This is what I did; I added a class to the elements I wanted to validate. Then I called the validate() on that specific class.

$(".elementClass").valid();

Upvotes: 0

Gregoire
Gregoire

Reputation: 3775

This method seems to do what you want:

$('#email-field-only').valid();

Edit

API has changed, see Paul's answer.

Upvotes: 159

Peps
Peps

Reputation: 59

in case u wanna do the validation for "some elements" (not all element) on your form.You can use this method:

$('input[name="element-one"], input[name="element-two"], input[name="element-three"]').valid();

Hope it help everybody :)

EDITED

Upvotes: 0

Dmitry Komin
Dmitry Komin

Reputation: 559

If you want to validate individual form field, but don't want for UI to be triggered and display any validation errors, you may consider to use Validator.check() method which returns if given field passes validation or not.

Here is example

var validator = $("#form").data('validator');
if(validator.check('#element')){
    /*field is valid*/
}else{
    /*field is not valid (but no errors will be displayed)*/
}

Upvotes: 8

Paul
Paul

Reputation: 20061

Use Validator.element():

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

Here is the example shown in the API:

var validator = $( "#myform" ).validate();
validator.element( "#myselect" );

.valid() validates the entire form, as others have pointed out. The API says:

Checks whether the selected form is valid or whether all selected elements are valid.

Upvotes: 33

aslanpayi
aslanpayi

Reputation: 845

$("#FormId").validate().element('#FieldId');

Upvotes: 23

Tracker1
Tracker1

Reputation: 19334

For some reason, some of the other methods don't work until the field has been focused/blured/changed, or a submit has been attempted... this works for me.

$("#formid").data('validator').element('#element').valid();

Had to dig through the jquery.validate script to find it...

Upvotes: 20

John
John

Reputation: 9

$("#element").validate().valid()

Upvotes: -7

ShaneBlake
ShaneBlake

Reputation: 11096

When you set up your validation, you should be saving the validator object. you can use this to validate individual fields.

<script type="text/javascript">
var _validator;
$(function () {    
     _validator = $("#form").validate();   
});

function doSomething() {    
     _validator.element($('#someElement'));
}
</script> 

-- cross posted with this similar question

Upvotes: 7

Related Questions