ASP.NET
  Home arrow ASP.NET arrow Page 2 - Understanding ASP.Net Validation Controls
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Understanding ASP.Net Validation Controls
By: Armando Andrade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 26
    2002-08-23

    Table of Contents:
  • Understanding ASP.Net Validation Controls
  • The Syntax Of Validation Controls
  • The RegularExpressionValidator Control
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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>


    More ASP.NET Articles
    More By Armando Andrade


     

    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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT