I have a form with two input textboxes, and I have included jQuery validation rules for both:
<script src="../../Scripts/jquery-validate/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#respondForm').validate({ onclick: false,
onkeyup: false,
onfocusout: false,
highlight: function(element, errorClass) {
$(element).css({ backgroundColor: 'Red' });
},
errorLabelContainer: $("ul", $('div.error-container')),
wrapper: 'li',
rules: {
'text': {
required: true,
minlength: 5,
maxlength: 10
},
integer: {
required: true,
range: [0, 90]
}
},
messages: {
'text': {
required: "xxx_Required",
minlength: "XXX Should be greater than 5",
maxlength: "XXX Cannot be greater than 10"
},
integer: {
required: "is required",
range: "is out of range: [0,90]"
}
}
});
});
</script>
</head>
.
.
.
<input type="text" id="text" name="text" />
<br />
<input type="text" id="integer" name="integer" />
<br />
<input type="submit" name="submit" value="Submit" />
<br />
I have used:
function(element, errorClass) {
$(element).css({ backgroundColor: 'Red' });
}
to highlight the error control. Now the problem is that in the following scenario, both the input textboxes remain highlighted (background color: red):
How do I resolve this problem?
Found the answer, you have to provide an unhighlight
property as well.
Adds the error class to both the invalid element and its label
$(".selector").validate({
highlight: function(element, errorClass) {
$(element).addClass(errorClass);
$(element.form).find("label[for=" + element.id + "]")
.addClass(errorClass);
},
unhighlight: function(element, errorClass) {
$(element).removeClass(errorClass);
$(element.form).find("label[for=" + element.id + "]")
.removeClass(errorClass);
}
});
More info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With