Click here to Skip to main content
12,998,363 members (61,910 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 28 Apr 2008

Phone TextBox Control with Built-in Phone Validator - ASP.NET Custom Control

, 28 Apr 2008
Rate this:
Please Sign up or sign in to vote.
Discussing the creation of custom Phone TextBox and its built-in PhoneValidator that validates a group of textboxes which represents three sections of a phone number. It inherits the BaseValidator and uses some new cool ASP.NET AJAX 3.5 features.


Sometimes it is troublesome to create a textbox for each section of a phone number and often, it requires a set of validators to validate a phone number that consists of three textboxes. In this case, as a developer, you will need to drag three textboxes to your page and drag a RequiredFieldValidator and a RegularExpressValidator for each textbox. Then, you need to specify "ControlToValidate", "Display", "ErrorMessage," "Text", ValidationGroup" and "ValidateExpression" (if it is a RegularExpression validator) properties for each validator. You could probably end up with some properties being set incorrectly after you went through all this trouble and try to test it. As a result of the time wasting experience, I decided to create a custom PhoneTextBox with built-in PhoneValidator control.


After I decided to create such server controls, problems started floating above the surface. The first thing that I found out is how to create a validator that would validate multiple controls. Secondly, it is a known fact that ASP.NET validators can only show one error message. With the design that I have in mind, I need to find a way to somehow plug in additional error messages for each scenario. In this case, I will need to be able to show two kind of messages in this PhoneValidator, such as InvalidPhoneNumber and MissingPhoneNumber.

To cut a long story short, I eventually figured out how to do it. If you would like to know in details, please go to my blog.

Using the Code

The way to use this control is just like any other stardard ASP.NET server control. To make the drag and drop controls from your own assembly, you need to add the following line to the web.config file under system.web/pages/controls:

<add tagPrefix="Library" namespace="Library" assembly="Library" /> 

Here is an example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 


<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
    #form {clear: both;}
    #form label {float: left;margin-right: 10px;}
    #form .phone {float: left;}
    #form br {float: left;}
    #form a {float: left;}
    <form id="form1" runat="server">
        <div id="form">
            <asp:scriptmanager id="ScriptManager1" runat="server"></asp:scriptmanager>
            <asp:validationsummary id="ValidationSummary1" 

			runat="server" validationgroup="test" />
            <label for="PhoneTextBox1">Phone</label>
            <library:phonetextbox id="PhoneTextBox1" 

		cssclass="phone" validationgroup="test" runat="server" 

		isrequired="True" /><br /><br />
            <asp:linkbutton id="LinkButton1" validationgroup="test" 

		runat="server" text="Submit" /> 


I wish this small amount of coding will help you in saving your time to do other more important programming. If you like this article, then please remember to vote. If you have any suggestions, bugs to report, or enhancements, please share them with me! Thanks!


  • 04/29/2008 - Uploaded the first edition


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Enoch Tsai
Software Developer (Senior) Bibles for America
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCustom PhoneTextBox Control Pin
JMWebstudios4-Nov-09 8:35
memberJMWebstudios4-Nov-09 8:35 
GeneralRe: Custom PhoneTextBox Control Pin
JMWebstudios4-Nov-09 8:53
memberJMWebstudios4-Nov-09 8:53 
GeneralBug fixes Pin
OriAlmog23-Oct-08 3:47
memberOriAlmog23-Oct-08 3:47 
GeneralRe: Bug fixes for Multiple Controls on Page Pin
JMWebstudios4-Nov-09 8:52
memberJMWebstudios4-Nov-09 8:52 
GeneralRe: Bug fixes for Multiple Controls on Page Pin
LMoody1-Feb-10 11:15
memberLMoody1-Feb-10 11:15 
Generalwell I find this useless... Pin
Seishin#28-Apr-08 23:12
memberSeishin#28-Apr-08 23:12 
AnswerRe: well I find this useless... Pin
Enoch Tsai29-Apr-08 5:29
memberEnoch Tsai29-Apr-08 5:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 28 Apr 2008
Article Copyright 2008 by Enoch Tsai
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid