5,117,952 members and growing! (13,764 online)
Email Password   helpLost your password?
Web Development » Validation » Validation Controls     Intermediate License: The Code Project Open License (CPOL)

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

By Enoch Tsai

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.
C# (C# 2.0, C# 3.0, C#), .NET (.NET, .NET 2.0, .NET 3.5, .NET 3.0), ASP.NET, Dev, Design

Posted: 28 Apr 2008
Updated: 28 Apr 2008
Views: 1,041
Announcements



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 1.69 Rating: 2.80 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
Library

Introduction

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 as result of the time wasting experience, I decided to create a custom PhoneTextBox with built-in PhoneValidator control.

Background

After I decided to create such server controls, problems start floating above the surface. The first thing that I found out is that 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 some how plug in additionl 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 make long story short, I eventually figure 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 drap and drop controls from your own assembly, you need to add the following line to 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style>
    #form {clear: both;}
    #form label {float: left;margin-right: 10px;}
    #form .phone {float: left;}
    #form br {float: left;}
    #form a {float: left;}
</style>
</head>
<body>
    <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" /> 
        </div>
    </form>
</body>
</html>
        

Conclusion

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, or enhancements, please share them with me! Thanks!

History

04/29/2008 - upload the first edition

License

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

About the Author

Enoch Tsai



Occupation: Software Developer (Senior)
Company: Bibles for America
Location: United States United States

Other popular Validation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
Generalwell I find this useless...memberSeishin#0:12 29 Apr '08  
AnswerRe: well I find this useless...memberEnoch Tsai6:29 29 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2008
Editor:
Copyright 2008 by Enoch Tsai
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project