I would really like use the jQuery Validation plugin in my ASP.NET Web Forms application (not MVC). I find it easier than adding asp validators everywhere and setting the control to validate field on all of them.
I am just having some issues both when setting the class like this class="required email" which I think has something to do with having a form tag within the main form tag.
I also run into issues when calling the jquery validate using the names which become mangled in an asp control
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
username: {
required: true,
minlength: 2
}, },
messages: {
username: {
required: "Please enter a username",
minlength: "username at least 2 characters"
},
}.
.......
<p>
<label for="username">
Username</label>
<input id="username" name="username" />
</p>
because this
<asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
renders as
<input name="ctl00$ContentPlaceHolder1$tbUsername" type="text" id="ctl00_ContentPlaceHolder1_tbUsername" />
and mangles the name. I can get the ClientID using <%=tbUsername.ClientID %>
but that doesn't work with ClientName
Has anyone had any success using the jquery validator plugin with asp.net? If so what about using multiple forms much like using separate validation groups?
You can checkout the rules add function, but basically here's what you can do:
This way no need to worry about the crappy identifiers generated by the webforms engine.
The best solution is use "<%=tbUsername.UniqueID %>" instead of tbUsername in jQuery rules.
For SharePoint 2010 I found with loading different usercontrols as views (via ajax) that this worked if you move javascript into a library and can't use server tags for the control id's like this:
e.g
#<%= tPhone.ClientID %>
Further to this if you dynamically load a user control via Ajax then you cannot use $(document).ready You will have to encapsulate the jQuery in a function library if its on the User Control at (server side event) page load its fine but in the scenario its loaded via Ajax with the Update Panel it will not dance.
I have not tried loading usercontrols via jQuery yet, this looks heavy and appears to load the whole page albeit perhaps slightly quicker or not.
Tests comparing loading techniques showed the Update Panel was as fast and resulted in the same or smaller page sizes than other techniques and basically loaded quicker or much more data as quick or quicker.
I recommend using jQuery.simple.validator, its easy, lightweigh and customizable validator compatible with asp.net web forms, because basically it can perform validations in any container, not only
https://github.com/v2msoft/jquery.simple.validator
I recommend you check the plugin and the documentation. Usage:
I recently posted a patch file for xVal.WebForms which resolves the multiple forms issue relaying on the well known ASP.Net validation Group. This patch also supports the ASP.Net CausesValidation property.
Yo can read about it here: http://cmendible.blogspot.com/