I love using the jquery validation plugin in my .NET projects; however, there are some things to keep in mind when using it – especially when used in conjunction with master pages.
What is that field ID?
We all know that master pages and content pages generate specific IDs for the form elements to ensure a truly unique ID Value. When using jQuery validate, you may be tempted to call the fields client id value from .NET as shown below.
$("#aspnetForm").validate({
rules: {
<%=txtName.ClientID%>: {
required: true
}
}
messages: {
<%=txtName.ClientID%>: "Please enter your name."
}
});
Trying to run this example will not work, that is because the validate plugin uses the "name” field of form elements to run it’s validation. Rewriting the javascript to use UniqueID instead would work.
$("#aspnetForm").validate({
rules: {
<%=txtName.UniqueID%>: {
required: true
}
}
messages: {
<%=txtName.UniqueID%>: "Please enter your name."
}
});
ASP.NET, jQuery, General
asp.net, jquery, tips