• Now Online : 19
  • admin@codemyne.net

Ensuring proper input from the user or verifying user entered input is called Validation. For example user name can not be blank, emailid requires proper format. ASP(before asp.net) requires manual coding to perform validations. The solution with asp.net is validation controls.
Validation controls will provide built in logic to validate user input at client side or server side.
validation controls will make developer job easier and development faster. Asp.Net providing following validation controls.

  • Range Validator
  • RequiredField Validator
  • Compare Validator
  • RegularExpression Validator
  • Custom Validator
  • Validation Summary

Common Properties Of Validation Controls

1. ControltoValidate: Specify standard webserver controlID to be validated with user input.Text: Specify text to be displayed if user input is invalid, this will be displayed with validation control.
2. Error Message: Specify error description to be displayed if user input is invalid, this will be displayed with validation summary control.
3. SetFocus On Error:
True: The cursor will be placed within same textbox if user input is invalid.
False: The cursor will be placed with in the next text box irrespective.
4. Enable ClientScript:
True: Client Side Validation.
False: Server side Validation

Overview Of Validation Controls:

1. Range Validator Control:

This control can be used to validate user input within particular range. This control requires minimum value and maximum value to verify user input.
Note: When client request comes to webpage, rangevalidator will download javascript to client browser.
When cursor is leading leaving textbox, javascript will be executed to validate user input into textbox, if it is invalid, text will be displayed and cursor will be placed within that textbox only. Button click will perform page submission if page validations are successful, otherwise page submission will not take place. The required javascript is attached to button by microsoft.

Note: Range validator will not perform validation if textbox is blank, this requires relating required field validator to range textbox.

 <asp:TextBox id="txtrangevalidate" runat="server" ValidationGroup="RangeValidator" />
                                                        <br />           
<asp:Button ID="Button1" Text="Submit" runat="server" ValidationGroup="RangeValidator"/>
                                                        <br />          
<asp:RangeValidator ID="RangeValidator1" ControlToValidate="txtrangevalidate" MinimumValue="2012-01-01" ValidationGroup="RangeValidator" 
                                                        MaximumValue="2012-12-31" Type="Date" EnableClientScript="false" Text="The date must be between 2012-01-01 and 2012-12-31!"
                                                        runat="server" />

2. Required Field Validator:

Required field validator can be used to verify whether user is providing input into the textbox or not, if user is not providing input text error message will be displayed.

<asp:TextBox id="txtName" runat="server" ValidationGroup="RequiredValidator" />
<br />           
<asp:Button ID="Button2" Text="Submit" runat="server" ValidationGroup="RequiredValidator"/>
<br />
<asp:RequiredFieldValidator ID="Requiredfield1" ControlToValidate="txtName" runat="server" ValidationGroup="RequiredValidator" ErrorMessage="Name is required"></asp:RequiredFieldValidator>

3. Compare Validator:

This control can be used to compare textbox data with a constant or another textbox using relational operator.

<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>                                
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="RegisterUserValidationGroup">Password required</asp:RequiredFieldValidator> </p>                        
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
<asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" 
ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">Confirm password required</asp:RequiredFieldValidator>
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="RegisterUserValidationGroup">Both passwords should be same</asp:CompareValidator>
<asp:Button ID="Button3" Text="Submit" runat="server" ValidationGroup="RegisterUserValidationGroup"/>

4. Regular Expression Validator:

This control can be used to validate user input againest a perticular expression. The expression can be framed using set of special chars.

1. \d{5}It is an expression to accept 5 digit number.
2. \d{1,5}It is an expression to accept number between one to five digits.
3. \d{1, }It is an expressio to accept numeric data with any number of digits.
4. \D{5}It is an expression to accept string with 5 characters.
5. \D{1,5}It is an expression to accept string between 1 to 5 characters.
6. \D{1,}It is expression to accept string with any number of characters.
7. {a,e,i,o,u}{3}It is a expression to accept string with 3 chars of vowels.
Ex: abc-invalid, aei-valid.
8. ^ It can be used to specify starting characters of string.
9. $ It can be used to specify ending characters of string.Ex: ^(http://)\D(1,)(.com/.net)$ http:\\xyz.com-valid

 <asp:TextBox id="txtZipcode" runat="server" ValidationGroup="ValidateZipcode"/>
<asp:RequiredFieldValidator ControlToValidate="txtZipcode"
ErrorMessage="Zipcode is required." ID="RequiredFieldValidator1" runat="server" 
ToolTip="Zipcode is required." ValidationGroup="ValidateZipcode">Zipcode required</asp:RequiredFieldValidator>
<asp:Button ID="Button4" text="Submit" runat="server" ValidationGroup="ValidateZipcode"/>
<asp:Label id="lbl" runat="server" />    
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationGroup="ValidateZipcode" 
ControlToValidate="txtZipcode"  ValidationExpression="\d{5}"  ErrorMessage="The zip code must be 5 numeric digits!"    runat="server" />

5. Custom Validator:

This control can be used to validate user input with the help of custom logic. The logic written by developer is called "custom logic". Custom validator is not providing built in logic to perform validation, developer should attach logic to custom validator to perform required validation. custom validator is of two types client validation and server validation. client validation function property requires javascript function to perform client side validation.

6. Validation Summary Control:

This control can be used to display all the error messages at one place.

For CustomValidator's server side validation, Validation Summary Control examples and to download source code, please see the following article.

Creating a Simple Registration or Signup and Login or Signin Form using Asp.Net, C#.Net and Sqlserver

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment

manthan 7/16/2013 (IST) / Reply


thty 7/20/2013 (IST) / Reply


Uknown 7/28/2013 (IST) / Reply

Thanks, Nice Article.

ravi 9/15/2014 (IST) / Reply

validation for voting card & driwing linces

dsf 11/9/2015 (IST) / Reply


kannan 5/14/2016 (IST) / Reply