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