Add/remove validation rules Dynamically
Posted
by
eddy
on Stack Overflow
See other posts from Stack Overflow
or by eddy
Published on 2010-12-25T18:38:12Z
Indexed on
2010/12/25
18:54 UTC
Read the original article
Hit count: 249
jQuery
|jquery-validate
Hi, I need to dynamically add validation rules to a text box when the user clicks on the checkbox in that row and remove it when the user unchecks it. This is what I did, and it works fine, but I'm not sure if it is the right way to do it. Here's my html code:
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectItems" value="<c:out value="${item.numberPlate}"/>" />
</td>
<td align="left"><c:out value="${item.numberPlate}"/></td>
<td align="left"><c:out value="${item.driver.fullName}"/></td>
<td align="left"><input type="text" name="mileage_<c:out value="${item.numberPlate}"/>" value="" /></td>
</tr>
</c:forEach>
</tbody>
and my jquery:
$("input[name=selectItems]").change(function() {
if (this.checked)
{
$(this).closest("tr").find("input[name^=mileage]").attr("class","required");
$(this).closest("tr").find("input[name^=mileage]").attr("number",true);
}
else
{
$(this).closest("tr").find("input[name^=mileage]").attr("class","")
}
});
any suggestion is welcome and... I almost forgot, Merry Xmas!
© Stack Overflow or respective owner