Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Label Placement using jQuery validate (For all or some of your errors)

I would like to place one error label (Not All) in a custom location. jQuery provides this http://docs.jquery.com/Plugins/Validation/validate#toptions option but I could not find anything about how to place one specific error message and not change the default location of all the rest?

Be sure to check all the answers below. There are multiple solutions to this. Thanks all!

like image 957
chainwork Avatar asked Nov 02 '12 17:11

chainwork


1 Answers

So if you want all your jQuery Validate error messages to appear in one place you would use http://docs.jquery.com/Plugins/Validation/validate#toptions (Find errorPlacement) option on that page.

I noticed some answers on here answer one but not both options.

1) That being said if you want custom placement for all of your errors you can do this:

$("#myform").validate({   errorPlacement: function(error, element) {      error.appendTo('#errordiv');    } }); 

2) If you want to specify specific locations for one or multiple error labels you can do this.

errorPlacement: function(error, element) {     if (element.attr("name") == "email" )         error.insertAfter(".some-class");     else if  (element.attr("name") == "phone" )         error.insertAfter(".some-other-class");     else         error.insertAfter(element); } 
like image 177
chainwork Avatar answered Sep 29 '22 14:09

chainwork