Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate, put messages before input box

I'm using this jQuery plugin: http://docs.jquery.com/Plugins/Validation/validate and it seems to always put the messages after the input element. is there a way to get them to append to before the element instead of after?

like image 385
GSto Avatar asked Sep 21 '11 17:09

GSto


2 Answers

Check errorPlacement: options for plugin:

$("#myform").validate({
  errorPlacement: function(error, element) {         
       error.insertBefore(element);
   }
 })
like image 111
Samich Avatar answered Oct 13 '22 10:10

Samich


Use errorPlacement to control will allow you to put error message on specific place.

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

Here are a few samples of errorPlacement with error options as well

  • error.appendTo(element.parent("div").next("div"));
  • error.appendTo($('#id'));
  • error.insertAfter(element);
  • error.insertBefore(element);

For custom error message and control creation

errorPlacement: function(error, element) {
    var elementForm = "", containerError = "";
    offset = element.offset();
    elementForm = element.attr("name");
    containerError = "#" + elementForm + "error";
    error.prependTo(containerError);
    error.addClass('message');
}
like image 39
ankitkanojia Avatar answered Oct 13 '22 12:10

ankitkanojia