Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query the validation of a specific field in javascript/jquery

With a simple MVC4 application I have a simple view with a simple form with some fields that have some simple validations (required, max/min, length, etc.). I have both ClientValidationEnabled and UnobtrusiveJavaSciptEnabled set to true.

What I want to do is, at any point in time, in javascript, ask if any one particular input field in my form will pass validation with the contents currently in the field.

I realize I can call $("#MyForm").valid() and know if the whole form itself passes validation. What I want to do is ask this question for any one particular field.

like image 333
Dan Avatar asked Dec 26 '22 07:12

Dan


1 Answers

You practically answered your own question :) Use .valid().

http://docs.jquery.com/Plugins/Validation/valid

You can call .valid() on elements directly.

$("#myElement").valid();

You can also trigger validation this way. http://docs.jquery.com/Plugins/Validation/Validator/element

$("#myForm").validate().element("#myElement");
like image 51
Snæbjørn Avatar answered Jan 31 '23 01:01

Snæbjørn