97

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.

Dawid Wekwejt
  • 533
  • 1
  • 4
  • 19
ryan
  • 971
  • 1
  • 6
  • 3

8 Answers8

156

This method seems to do what you want:

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

Edit

API has changed, see Paul's answer.

Gregoire
  • 3,735
  • 3
  • 25
  • 37
  • 4
    Note: form element name also works with this function, i.e. `$('input[name=email-field-only]').valid();` also works – Raptor Nov 22 '12 at 07:17
  • 1
    what is the problem? when I use this method my validation breaks – nakajuice Jul 03 '13 at 15:21
  • 2
    This solution validate the whole form, showing the error messages for all fields. – Pablo Apr 21 '15 at 13:21
  • @haemhweg, maybe you didn't call the *.validate()* method before trying to use the *.valid()* ? The docs: https://jqueryvalidation.org/valid/ – userfuser Oct 10 '16 at 08:32
  • This works but I have an issue checking twice... it gives me JS error: TypeError: validator is undefined.. To replicate, do this: 1) fail the validation; 2) put correct value in field; 3) re-validate – phoenix Nov 28 '16 at 19:47
  • 2
    The API has changed, use the [answer provided by Paul](https://stackoverflow.com/a/32146028/3163075) – Anima-t3d Oct 05 '17 at 04:02
33

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.

Paul
  • 19,704
  • 14
  • 78
  • 96
22
$("#FormId").validate().element('#FieldId');
aslanpayi
  • 805
  • 7
  • 10
20

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...

Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • 1
    This is what worked for me, I also added `.valid()` at the end so it looked for me `$("#Form").data('validator').element('input[name=__Suburb]').valid();` I believe that if you do not select it by ID this might be required. – Mihai P. Jan 15 '15 at 00:10
  • 4
    I don't think `.valid()` belong at the end of the expression. The `.element()` function returns a boolean result ([docs](https://jqueryvalidation.org/Validator.element/) | [src](https://github.com/jquery-validation/jquery-validation/blob/1.16.0/dist/jquery.validate.js#L503)). When I try to call .valid() I get the error message: *"Uncaught TypeError: `$(...).data(...).element(...).valid` is not a function"* – KyleMit Apr 19 '17 at 15:29
  • Did you attach the validator to the form? it's been a few years since I've used JQuery validation... So the API for this may have changed. At the time I had to work through internals to find the above it. It wasn't published in the documentation so the structure may well have changed since I last looked. – Tracker1 Apr 21 '17 at 11:26
8

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)*/
}
Dmitry Komin
  • 549
  • 6
  • 7
7

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

Community
  • 1
  • 1
ShaneBlake
  • 11,056
  • 2
  • 26
  • 43
0

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

Peps
  • 49
  • 1
  • 4
-7
$("#element").validate().valid()
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
John
  • 9