jQuery Validation - error placement

Iladarsda picture Iladarsda · Jul 1, 2011 · Viewed 80.4k times · Source

I'm trying to use 'errorPlacement' from jQuery Validation DOCS:

$("#myform").validate({
  errorPlacement: function(error, element) {
     error.appendTo( element.parent("td").next("td") );
   },
   debug:true
 })

I want to place error before not valid input, but this does not work:

$("#myform").validate({
      errorPlacement: function(error, element) {
         error.appendTo( element.parent("form").prev("input") );
       },
       debug:true
     })

LIVE DEMO

Any help much appreciated!

Answer

Frédéric Hamidi picture Frédéric Hamidi · Jul 1, 2011

Since you want to insert the error message before the invalid element, use insertBefore():

errorPlacement: function(error, element) {
    error.insertBefore(element);
}