Is it possible to know with jQuery-validate when one field check failed

I have a form where I need to perform additional processing when one field check failed (when the user selects outside the field, for example, but the form was not submitted), how can I connect to this event using JQuery-Validate?

+3
jquery jquery-validate
May 05 '11 at 13:41
source share
3 answers

To find out if there was a form error, use:

if(!$("#form0").valid()){ //There was one or more errors in the form } 

To find out if a particular element has an error, use:

 if(!$("#form0").validate().element($("#text1"))){ //There where some error in #text1 } 

(Note that these two methods also trigger checks)

Hope this helps. Greetings

+3
May 05 '11 at 1:59 pm
source share

I use a combination of overriding the most highlighted / unlit.

 //Update the validator highlight/unhighlight $.validator.setDefaults({ ignoreTitle:true ,highlight: function (element) { var el = $(element); //TODO: Handle UI changes, add/remove classes el.trigger("validate.fail"); } ,unhighlight: function (element) { var el = $(element) //TODO: Handle UI changes, add/remove classes el.trigger("validate.success") } }); 

Now I can just bind the validate.fail method ...

 $("#myInputElement").bind("validate.fail",function(){ //TODO: Do something with this knowledge. }); 



NOTE. I have already done this in the past to integrate jQuery with bootstrap user interfaces ... it worked very well.

+2
Aug 07 2018-12-12T00:
source share

When setting up validation, you must save the validator object. you can use this to check individual fields.

 <script type="text/javascript"> var _validator; $(function () { _validator = $("#form").validate(); }); function doSomething() { _validator.element($('#someElement')); } </script> 
+1
May 05 '11 at 13:56
source share



All Articles