I am trying to use JQuery Validation plugin on a page where i need to validate a input fields on each row of a table. these input fields doesn't share same name. so i added validation messages to input fields using the addMethod and addClassrules.
$.validator.addMethod("RequiredFields", $.validator.methods.required, "Required Field");
$.validator.addMethod("EmailValid", $.validator.methods.email, "Invalid Email");
$.validator.addClassRules("RequiredFieldValidation", {
RequiredFields: true
});
$.validator.addClassRules("EmailValidation", {
RequiredFields: true,
EmailValid: true
});
$("#Form1").validate({
errorPlacement: function (error, element) {
if (element.parent("td").parent("tr").find('td:last-child').html() == "") {
element.parent("td").parent("tr").find('td:last-child').html("*");
error.appendTo(element.parent("td").parent("tr").find('td:last-child'));
} else {
element.parent("td").parent("tr").find('td:last-child').append(" and ");
error.appendTo(element.parent("td").parent("tr").find('td:last-child'));
}
},
errorClass: "ErrorCell",
validClass: ""
});
i have added RequiredFieldValidation
and EmailValidation
as classes to the input fields.
My HTML looks like below.
<pre>
<code>
<form id="Form1" method="post" runat="server">
<table id="tbl_Temp">
<tbody>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th class="ErrorCellHeader"></th>
</tr>
<tr>
<td>
<input name="txtFName1_Temp" id="txtFName1_Temp" />
</td>
<td>
<input name="txtLName1_Temp" class="RequiredFieldValidation " id="txtLName1_Temp" />
</td>
<td>
<input name="txtEmail1_Temp" class="EmailValidation" id="txtEmail1_Temp" />
</td>
<td class="ErrorMessage"></td>
</tr>
<tr>
<td>
<input name="txtFName2_Temp" id="txtFName2_Temp" />
</td>
<td>
<input name="txtLName2_Temp" class="RequiredFieldValidation " id="txtLName2_Temp" />
</td>
<td>
<input name="txtEmail2_Temp" class="EmailValidation" id="txtEmail2_Temp" />
</td>
<td class="ErrorMessage"></td>
</tr>
</tbody>
</table>
<input name="btn_submit" id="btn_submit" type="submit" value="Submit" />
</form>
</code>
</pre>
Everything works fine..except the error messages. i am trying to concatenate messages with "and" but i can't get rid off them when the validation passes.
Any inputs are appreciated.
Demo can be found at JSFiddle