Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validation plugin in ASP.NET Web Forms

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?

like image 400
eiu165 Avatar asked Mar 06 '09 18:03

eiu165


People also ask

How we can use jQuery validation plugins in MVC?

The jQuery validation plugin leverages a CSS selector like syntax to apply a set of validation rules. You can download the plugin (js) file from jQuery website. The password and confirm password objects are matched by the validation plugin for you and shows the message on the equalTo attribute if they don't match.

What is form validation in jQuery?

Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.

How can use client side validation in jQuery MVC?

The above properties are set True by default which means MVC 5 platform ensures that client side validation on form validation is on. For jQuery form validation to work, we set "HtmlHelper. UnobtrusiveJavaScriptEnabled = false;" property false in the register form instead of "web.


2 Answers

You can checkout the rules add function, but basically here's what you can do:

jQuery(function() {     // You can specify some validation options here but not rules and messages     jQuery('form').validate();      // Add a custom class to your name mangled input and add rules like this     jQuery('.username').rules('add', {          required: true,          messages: {              required: 'Some custom message for the username required field'          }      }); });  <input name="ctl00$ContentPlaceHolder1$tbUsername" type="text" id="ctl00_ContentPlaceHolder1_tbUsername" class="username" /> 

This way no need to worry about the crappy identifiers generated by the webforms engine.

like image 117
Darin Dimitrov Avatar answered Oct 05 '22 23:10

Darin Dimitrov


Here are examples of using the jQuery Validation plugin with WebForms and emulating the concept of validation groups with it. It actually works pretty well once you smooth out a couple issues.

like image 45
Dave Ward Avatar answered Oct 06 '22 00:10

Dave Ward