Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 

Ads Via DevMavens


Validating Phone Numbers with Extensions in ASP.NET

Posted by on Saturday, October 02, 2004

There is no built in validation control to handle phone numbers with an optional extension. This article will show you how.
In Visual Studio .NET, we are fortunate to have Form Validation controls which do an excellent job for us. But, with Regular Expression Validators, Microsoft has only given us a taste of the real power behind this control. To be able to take full advantage of the Regular Expression Validator control requires some “programming”. Here is a simple example: When we want to validate a U.S. phone number, the Regular Expression validates the area code, a three digit number, a “dash”, and then a four digit number. This works fine if we were designing a web app for home use, but what about a business application where we need phone numbers with extensions? This is where the “programming” comes in. I have taken care of the phone number + extension issue for you.

This is what your ValidationExpression property should look like:  

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}( x\d{0,})?

 Copy and paste this string into the ValidationExpression property of your Regular Expression Validator control. What I did was add “( x\d{0,})?” to the end of the string (without the quotes) that Microsoft provided for us. My example allows a user to enter a phone number with an extension formatted as: (770)123-4567 x1234. If you prefer to use “ext” for extension, simply edit the above string and replace “x” with “ext”. Make sure that your database field is big enough to accommodate the entire phone number and extension – at least varchar(25) in our case.

 

So how did I figure out what characters to use to create this validation string? It’s called Regular Expressions. Check out or search for this excellent tutorial on Microsoft’s MSDN site: Regular Expressions in ASP.NET

 

by Carl Ernest


Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
Databinding in Silverlight
This article will cover data binding features in Silverlight, including binding expressions, validation, converters, and binding modes.

The Standard LINQ Operators
This article will cover the standard LINQ operators provided by LINQ for filtering, grouping, joining, converting, projecting, and more.

C# 3.0 and LINQ
C# 3.0 introduced a number of new features for LINQ. In this article we'll examine the new features like extension methods, lambda expressions, anonymous types, and more.

Most Popular Articles
Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

AppSettings In web.config
In this article we will review a couple of pratices to keep your runtime configuration information flexible.

Contribute Code
Privacy
Consultancy