I'm using jquery validator to validate a field in my form:
$.validator.addMethod('positiveNumber',
function (value) {
return Number(value) > 0;
}, 'L\'année ne peut pas être négative.');
$('#leapyear').validate({
errorPlacement: function(error, element){
error.appendTo(element.parent("td").next('td'));
}
});
The form looks like this:
<form id="leapyear" name="leapyear" action="Calculateur-Dates.html" method="post">
<table style="padding-left: 70px; width: 100%">
<colgroup>
<col width="25%">
<col width="15%">
<col width="20%">
<col width="40%">
</colgroup>
<tr>
<td class="calc_label">Year to be checked</td>
<td>
<input class="calc_input required positiveNumber" size="10" type="text" value = "2012" name="leap_year" id="leap_year" /></td>
<td class="validatorErrMsg"></td>
<td id="checkResult" class="calc_resMsg green">2012 is a leap year</td>
</tr>
<tr>
<td>
</td>
<td>
<center><button style="padding-left: 10px" id="leapYearBtn" name="submit" class="lnk_button" value="vérifier">vérifier开发者_运维百科</button></center>
</td>
<td colspan="2">
</td>
</tr>
</table>
</form>
The check works fine, however I would also like the script to erase text from td#checkResult when raising an error. I am not sure if that is possible and, if so, how to proceed. Is there a way to do this ?
Thanks in advance
you should be able to just do this
$('td #checkResult').html('""');
Thanks to the code line from Mark, I thought about looking at the problem from a different angle and added a separate jQuery function for this:
$('#leapYearBtn').click(function(){
$('td#checkResult').html('');
});
which does what I intended it to do.
Try using the invalidHandler, get the number by using validator.numberOfInvalids(), if greater then 0, clear $('#checkResult').html('');
$('#leapyear').validate({
errorPlacement: function(error, element){
error.appendTo(element.parent("td").next('td'));
},
invalidHandler: function() {
if (validator.numberOfInvalids() > 0)
$('#checkResult').html('');
}
});
精彩评论