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?
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?
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.
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();
}
}
})
$("#myform").validate({
errorPlacement: function(error, element) {
error.appendTo( $('input#test'));
}
})
$("#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.