views:

794

answers:

4

I am using jQuery validation plugin and I want that the error message to be displayed in a specific text input with id="test".

How do I do this?

A: 

Looking at the documentation, you could find out if you have any errors by querying numberOfInvalids( )

var validator = $("#myform").validate({
  invalidHandler: function() {
    $("#summary").text(validator.numberOfInvalids() + " field(s) are invalid");
  }
});

and if you have any, you could then go through the elements in your form individually

$("#myform").validate().element( "#myselect" );

This code returns a boolean as to whether the element is valid or not.

David Christiansen
+1  A: 

From the Validation Plugin documentation:

$(".selector").validate({
    invalidHandler: function(form, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = errors == 1
          ? 'You missed 1 field. It has been highlighted'
          : 'You missed ' + errors + ' fields. They have been highlighted';
        $("div.error span").html(message);
        $("div.error").show();
      } else {
        $("div.error").hide();
      }
    }
 })
Tony Miller
A: 
$("#myform").validate({
  errorPlacement: function(error, element) {
     error.appendTo( $('input#test'));
   }
 })
Daniel Moura
A: 
    $("#myform").validate({
        errorPlacement: function(error, element) {
            $('input#test').val(error);
        }
    });

Although, I'm not sure why you'd want to populate an input field with the validation error, but i'm sure it makes sense on the front end you're designing.

FerrousOxide