ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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 
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 RegularExpressionValidator Control


    (Page 3 of 4 )

    The next Control is the RegularExpressionValidator control. This control makes use of regular expressions, which are a way of describing a string pattern. Regular expressions are out of the scope of this article, so we will bypass this control. Just remember that this control exposes the ValidationExpression property, where you can enter the RegularExpression you want to valid a control against.

    There are going to be cases where you need a special way of validate some content: something that the .NET Framework can't offer to you. This is where CustomValidator control comes into play.

    As the name implies, this control lets you write the logic for the validation of a control anyway you like. This control exposes an OnServerValidate event that you can use to tie the logic written on the server (such as a function) to the validation control, meaning that the validation will be performed on the server.

    Both the server and client validation have to have specific formats. On the server side if you are using C# then the function must return a void and have two parameters: an object argument and a System.Web.UI.WebControls.Custom.ServerValidateEventArgs object.

    Here's a function that checks if a number is a pair or not. As you can see, we don't perform any checking in the function about the type of data we are receiving in the argument, since we already saw how to accomplish this using the other Validation controls:

    public void Doble(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
    {
    try
    {
    if ((int.Parse(e.Value) % 2) == 0)
    e.IsValid = true;
    else
    e.IsValid = false;
    }
    catch
    {
    e.IsValid = false;
    }
    }


    Here you can see that we check if the modulus of the value being passed to the function – if the modulus is zero then it's a pair number. If anything goes wrong the catch block sets the IsValid property to false.

    The only drawback to this method is that the validation is made on the server, so you need a roundtrip to the server each time you want to perform the validation. In order to avoid this, you have to code a client side version of our validation code using JavaScript.

    A nicer control is the ValidationSummary control, which doesn't actually perform any validation – this control makes it possible to display all of the error messages in a single place, making a nice layout for your page.

    By default all of the validation controls display their error message where the control is placed, so the ValidationSummary control serves as a way of organize the error messages of the controls. Remember, however, that by using the Text property of the controls, you can still mark an astricks in place where an error is and display a more detailed message in the summary control.

    The summary control has different rendering options, such as a bulleted list, a single paragraph or a simple list. It also has the option to be displayed as a message box if JavaScript is enabled.

    If you want to have code reusability in your applications, then you could create a control that contains a textbox for email, a RequiredFieldValidator control, and a RegularExpressionValidator control. You could then put this combination of controls in the VS.NET listbox and use it like a drag 'n drop control in your webforms.

    Finally, remember to use the Page.IsValid property before performing any logic, as this property is set on a page when the page uses a validation control. After a PostBack event, this property will be set to true only if the page passed all of the validation logic required.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT