VisualBuilder
  Home > Aspnet > Tutorials > Validation Controls - ASP.NET Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
ASP.NET Tutorial Home
1 . Introduction
2 . Static and Dynamic Web sites
3 . Dynamic Web sites
4 . What is ASP
5 . How ASP Works
6 . What is ASP.NET
7 . What is a Framework?
8 . ASP.NET Versions
9 . The .NET Framework architecture
10 . Important Terminologies
11 . Why Use ASP.NET
12 . Setting up ASP.NET
13 . Installation of .NET Framework 2.0
14 . Installing the Software Development Kit (SDK)
15 . Configuring IIS for ASP.NET 2.0
16 . Developing your First ASP.NET Page
17 . Understanding the ASP.NET Code
18 . Next Steps
19 . Asp.Net
20 . DataList
21 . Validation Controls
22 . ExecuteNonQuery
23 . ADO.NET
24 . Using Parameters with Queries
25 . EXECUTE SCALER: Retrieving Single Database Record
26 . Improving Database Performance Using Stored Procedure
27 . Example for XML Handling Using ASP. Net.
28 . CACHING IN ASP.NET APPLICATION
29 . GridView Control
30 . Master-Detail Relationship: Using DATALIST and REPEATER Control
31 . Data Binding – WebControls [REPEATER CONTROL]
32 . Session Management in Asp.Net
 
Aspnet Group Home
Aspnet Discussion (10)
Aspnet Members (2382)
Aspnet Resources
Aspnet Source Code (388)
Aspnet Articles (1)
Aspnet Blogs
Aspnet Jobs
Aspnet Components (201)
Aspnet Books
Aspnet Websites (21)
Aspnet News (105)
Aspnet Q & A (114)
- Aspnet Ask Question
- Aspnet Questions
- Aspnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (434 )
Source Code (3275 )
Articles (11 )
Components (1595 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Aspnet Tutorial
 Validation Controls
  << Prev: DataList Next: ExecuteNonQuery >>

Validation can be applied on both client-side and server-side. The widely used client-side validation is javascript. Asp.Net validation controls provide two types of validation i.e Server and client side. These validation controls will perform client-side validation when they detect the browser is able (unless client-side validation has been disabled). This will reduce extra roundtrip to the server. And this will perform server side where ever necessary.


 


There are 6 validation controls.



  1. RequiredFieldValidator Control

  2. CompareValidator Control

  3. RangeValidator Control

  4. RegularExpressionValidator Control

  5. CustomValidator Control

  6. ValidationSummary


 


 


BaseValidator Class: All validator controls are inherited from BaseValidator class so they have enjoyed some common methods and properties.



  • ControlToValidate - This attribute contains the ID of the control that need to be validate.

  • ErrorMessage - Error message that will be displayed in the validation summary.

  • IsValid -This provide the check whether the control is valid or not.

  • Validate - Method to validate the input control and update the IsValid property.

  • Display - This attribute is responsible to display/shown the error message. The options are none, static, dynamic.


RequiredFieldValidation Control



This control will check whether the user input any value in the control that we are validating or not.


 


Eg.


<asp: textbox id=”txtUserrunat=”server/>


<asp: RequiredFieldValidator id=”valReqUserrunat=”serverControlToValidate=”txtUserErrorMessage=”UserName is RequiredDisplay=”dynamic>


</asp:RequiredFieldValidator>


 


CompareValidator Control


This control is used to compare the data of two controls.


 


Eg.


<asp: textbox id=”txtPasswordrunat=”server/>


<asp: textbox id=”txtConfirmPasswordrunat=”server/>


<asp: CompareValidator id=”valComparerunat=”serverControlToValidator=”txtPasswordControlToCompare=”txtConfirmPasswordOperator=”EqualsErrorMessage=”Please insert same values.Display=”dynamic>


</asp: CompareValidator>  


 


Here the Operator contains options like equal, greaterthanequal, less than etc. We can also compare the control value with the numeric, string, currency, double and date values using Type attribute of the CompareValidator.  


 


  RangeValidator Control:


This control checks whether the control value which we are going to validate is within the valid range that is specified or the user input value is between specified lower and upper boundaries.  


 


<asp: textbox id=”txtAgerunat=”server/>


<asp: RangeValidator id=”rngValControlToValidate=”txtAgeMinimumValue=”20MaximumValue=”30Type=”IntegerError Message=”Age can't be less than 20 and greater than 30Text=”Age doesn't met the criteriarunat=”server />  


 


The ControlToValidate attribute specify the control name that we need to validate, the maximum and minimum value define the boundaries for the value that is inserted in the control and Type define the data type , type can be Integer, String, Double, Date , Currency.  


 


RegularExpressionValidator Control


 


This control is used for matching user input against a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as e-mail address, social security numbers, telephone numbers, and postal codes and so on.  


 


<asp: textbox id=” txtzipcode runat=”server”/>


<asp: RegularExpressionValidator id=”rgExpValControlToValidate = “txtzipCodeValidationExpression = “\d{5}Error Message=”The zip code must be 5 numeric digits!.Text=” The zip code must be 5 numeric digits!.Runat=”server />  


Use the ValidationExpression property to specify the regular expression used to validate input control. The regular expression validation syntax is slightly different on the client than on the server.On the Client, Jscript regular expression syntax is used. On the server Regex syntax is used. Note: Jscript is subset of Regex syntax, so use Jscript regular expression in order for same results on both client and server side.  


CustomValidator Control:


The control is used for defining custom server and client validation code.  


<script runat=”server>


Sub ServerValidateFunction(s as Object,arg as ServerValidateEventArgs)


If len (arg.Value) < 8 or len (arg.Value) > 16 Then


arg.IsValid = False


Else


arg.IsValid = True


End If  


</script>


<asp: Label runat="server" Text="Enter a username: " />


<asp: Textbox id="txtUserName" runat="server" />


<asp: Button Text="Submit" runat="server"/>


 <asp: CustomValidator id=”val1ControlToValidate=”txtUserNameClientValidationFunction=”” OnServerValidate = “ServerValidateFunctionErrorMessage=” A username must be between 8 and 16 charactersText=” A username must be between 8 and 16 charactersRunat=”server />


 


This control allow you to create a validation control with its validation handler. To create a server-side validation function, Provide a handler for the ServerValidate event that performs the Validation.The string from the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter.The result is then stored in the Is Valid property of the ServerValidateEventArgs object.


 


Note:- To create a client-side validation function, first add the server-side validation function described earlier. Next, add the client-side validation script function to the .aspx page.


 


Validation Summary



Display a list of all validation errors on the Web Page. This will help in summarize the entire error message at one location.  


<asp:ValidationSummary id=”IDDisplayMode = “BulletList | List | SingleParagraphEnableClientScript = true| False ShowSummary = true | False ShowMessageBox = true | False HeaderText =”Text To display as summary TitleRunat=”server/>

  << Prev: DataList Next: ExecuteNonQuery >>
Aspnet Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Skype vs. sipcall

 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising