I've a custom method for jQuery validate (based on Sparky's answer for a related question) that checks to make sure that one from a group of four text fields has a value in it. This works fine, except I've just noticed that even though the error message disappears when a value has been entered in one of the fields, the error class remains in place so the text fields highlighted in the error colour (red background in this example).
Any ideas on how this can be fixed?
Thanks.
Here's a jsfiddle of the issue.
Here's my script:
$(document).ready(function () {
$.validator.addMethod("textboxrule", function (value, element) {
return ($('#my_text_field_1').val() != '' || $('#my_text_field_2').val() != '' || $('#my_text_field_3').val() != '' || $('#my_text_field_4').val() != '')
}, "Select at least one of these four");
$('.my_form').validate({ // initialize the plugin
errorLabelContainer: ".form_errors",
wrapper: "li",
groups: {
somename: "my_text_field_1 my_text_field_2 my_text_field_3 my_text_field_4"
},
rules: {
'my_text_field_1': {
textboxrule: true
},
'my_text_field_2': {
textboxrule: true
},
'my_text_field_3': {
textboxrule: true
},
'my_text_field_4': {
textboxrule: true
}
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
});
Here's my HTML:
<form class="my_form" method="post" action="#">
<div>
<ul class="form_errors"></ul>
</div>
<p>
<label>My text field 1</label>
<input id="my_text_field_1" name="my_text_field_1" type="text" value="" /></p>
<p>
<label>My text field 2</label>
<input id="my_text_field_2" name="my_text_field_2" type="text" value="" />
</p>
<p>
<label>My text field 3</label>
<input id="my_text_field_3" name="my_text_field_3" type="text" value="" /></p>
<p>
<label>My text field 3</label>
<input id="my_text_field_4" name="my_text_field_4" type="text" value="" /></p>
<input type="submit" value="Submit" />
</form>
Here's my CSS:
input.error {
background: red;
}
require_from_groupmethod from theadditional-methods.jsfile. Then this additional error message manipulation would have been unnecessary.