Home arrow ASP.NET arrow Page 2 - Understanding ASP.Net Validation Controls
ASP.NET

Understanding ASP.Net Validation Controls


Finding input validation with ASP.Net a chore? In this article Armando gives us the low down on ASP.Net validation controls, along with helpful coding examples to get you started.

Author Info:
By: Armando Andrade
Rating: 4 stars4 stars4 stars4 stars4 stars / 65
August 23, 2002
TABLE OF CONTENTS:
  1. · Understanding ASP.Net Validation Controls
  2. · The Syntax Of Validation Controls
  3. · The RegularExpressionValidator Control
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Understanding ASP.Net Validation Controls - The Syntax Of Validation Controls
(Page 2 of 4 )

The syntax of these validation controls is very simple and you don't have to deal with complicated JavaScript validation code. All of the Validation controls inherit from the Label control.

Here are some of their most important properties:
  • ControlToValidate: The ID of the control that this validator validates.
  • Display: Has three possible values: Dynamic, Static and None. Dynamic means that the space the control uses isnít reserved for the control. Static means that the space it uses is always reserved for the control, and lastly None makes the control invisible.
  • EnableClientScript: This means that the validation occurs on the server (the default is true).
  • Text: Is displayed when the validation fails; itís often used to put an asterisk or an icon next to the error and for displaying the error message in a validation summary.
Remember that if you have VS.NET you don't need to hard code all of these properties; just open the properties dialog in Visual Studio using the design mode.

The simplest control is the RequiredValidator Control, which as the name implies, looks for the specified control to validate and checks if it contains a specific value.

It compares the value of a control with its InitialValue property (which defaults to an empty string). If the two values are the same then it evaluates to false. If the InitialValue property isn't an empty string, a blank entry would be valid, so the simplest way to avoid this is with the use of two controls.

Here's an example of using the control with a Textbox:

Email: <asp: Textbox runat="Server" ID="email" />

<asp:RequiredFieldValidator runat="Server" ControlToValidate="email" ErrorMessage="Please enter an email address" ID="emailReqValidator" />


The CompareValidator Control
The next control is CompareValidator, which you use to compare a control's value with a static value or another control's value. This control exposes four properties to define how the comparison is performed:
  • Type: Describes the type of data expected in the ControlToValidate.
  • Operator: Determines the type of operation the control is going to perform. A very important option is DataTypeCheck, which will be used to check if the value in the Type property is correct.
  • ControlToCompare: The ID of the control to compare with.
  • ValueToCompare: The static value to compare with the ControlToValidate.
Remember that only one instance of the last two properties can be given to a single control.

Here's an example that uses the CompareValidator control. We are using it to make sure the first textbox has an integer value, and then we use it to ensure that the first textbox value is greater than the second.

Here's the code:

<asp:TextBox id="Num1" runat="server"></asp:TextBox>
<asp:TextBox id="Num2" runat="server"></asp:TextBox>

<asp:CompareValidator id="CompareValidator1" runat="server" ErrorMessage="You have to write an integer value" ControlToValidate="Num1" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>

<asp:CompareValidator id="CompareValidator2" runat="server" ErrorMessage="The 1st value have to be greater than the 2nd" ControlToValidate="Num2" Type="Integer" ControlToCompare="Num1" Operator="LessThan"></asp:CompareValidator>






The next control is RangeValidator, which is a little simpler than the CompareValidator control. This control simply checks that the value falls into a specific range.

This range is determined from the MaximumValue and MinimumValue properties; this control also contains the Type property, just like the CompareValidator control.

Here's an example of using the RangeValidator control to ensure that the value in the textbox falls between 100 and 300:

<asp:textbox id="weight" runat="server"></asp:textbox>

<asp:rangevalidator id="RangeValidator1" runat="server" ErrorMessage="Write a correct weight" ControlToValidate="weight" Type="Integer" MaximumValue="300" MinimumValue="100"></asp:rangevalidator>



blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials