Add/remove validation rules Dynamically
- by eddy
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!