Introduction
In general there are some controls called 'validation controls' in asp.net to validate controls. One should drag and drop these controls on to source page and ofcourse 'ControlToValidate' property is to be filled with required control.There is an alternate way to validate controls is usign Jquery plugin 'jquery.validate.js'.

A single line of Jquery to select the form and applying the plugin is enough to validate the controls. There are two things one should to know before using this plugin.
- methods
- rules
method: A validation method implements the logic to validate an element, like an email method that checks for the right format of an text input's value.
rule: A validation rule associates an element with a validation method, like "validate input with name "primary-mail" with methods "required" and "email".
Start Validation: To start validating the controls
give reference to latest jquery file and jquey.validate.js file. You can download these file from JQery site.
Before validating controls,add your form to validate method in document.ready function as shown below.
This method sets up event handlers for submit, focus, keyup, blur and click to trigger validation of the
sentire form or individual elements.
<script> $(document).ready(function(){ $("#Form1").validate(); }); </script>
Next use rules and messages to specify which elements to validate, and how as explained below.
Add a control to your webpage. And set it class with rule 'required'. This Makes the element always required.
<input id="fname" name="fname" size="25" class="required" minlength="2" />
Email Validation: To validate the input control with eamil set it's class to 'email' rule as shown below
<input id="femail" name="email" size="25" class="required email" />
Compare two input controls: To compare two input controls equalTo method is used. Returns true if the value has the same value as the element specified by the first parameter. Works with text inputs. This method can be used as shown below.
$("#Form1").validate({ rules: { password: "required", password_again: { equalTo: "#password" } } });
There are set of methods and rules are available to validate controls in easy manner. For more information click here
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment
d 12/23/2011 (IST) / Reply
xzc