Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Phone number Regular Expression Validators

Posted on 2010-09-20
Medium Priority
Last Modified: 2012-05-10
Hi There

I am trying to force a user to insert a number in a specified format.

Our cell numbers in south africa is in the following format
I need them to write the number as

I currently have a kasked extender that works like this
<ajaxToolkit:MaskedEditExtender runat="server" ID="mee" ClearMaskOnLostFocus="false" TargetControlID="txtCell" Mask="+27999999999"></ajaxToolkit:MaskedEditExtender>

But the users are still entering the nu,mbers incorrectly.

I need a regular expression that there is no 0 after the 27 and that the amount of digits is 11 exluding the +


Question by:Stanton_Roux
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 33715786
A couple of points:
1 - you can't do that with Microsoft's ajax toolkit - the format descriptors aren't rich enough to define something as being 1-9 (and not 0).  You'd get closer with "\+\2\799999999", but this still wouldn't stop people entering +240...
2 - the masked edit extender relies on javascript.  If a user's browser doesn't have javascript, or the user has disabled it, then you get no mask applied anyway.

You'd do better letting your users enter a number in a way that makes sense to /them/, and then parsing the number to get the format /you/ want.  You could do this client-side with javascript, but as per note 2 above you'd have to be able to do it server side as well for non-javascript browsers.

LVL 13

Expert Comment

ID: 33715852
you can use the asp:regular expression control.

Here is the regex you are looking for:



LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33715862
You can try this
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 33716008
This pattern should suit you:

Open in new window

LVL 31

Expert Comment

by:Marco Gasi
ID: 33716079
I agree with jon47: let your user type phon number as they wish, post the form and receiving the form formta number as you wish.
Using php you can do this way:

$phone_number = $_POST['phone_number'];
//$phone_number = "0845678890";
$phone_number = preg_replace("/\b0/", "", $phone_number);
$phone_number = "+27".$phone_number;
if (strlen($phone_number)>12){
    echo "Invalid phone number.";
}else echo $phone_number;
LVL 13

Expert Comment

ID: 33716331
I misread your question, I thought you wanted 11 including the + not excluding..

Modification of my previous post:

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33717334

It's a bit redundant to put a single character in a character class--just have the character as the pattern itself. Also, your pattern is omitting zero as a valid number in the phone number. Only the first digit after "27" is not supposed to be zero, unless I am reading the question wrong  :)

I guess in hindsight, the lookahead wasn't really necessary in my post. The pattern could simply be:

Open in new window

LVL 13

Expert Comment

ID: 33717370
Thanks kaufmed. RegEx is something I'm trying to brush up on. (thus why i'm trying to participate is these kind of questions more.

what does
^\+27 mean?

I know ^ is the beginning of the line.

but what does the \ before "+27" do?

LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33717483
Plus (+) is a special character in regex (one-or-more). Since you want to find a literal plus, you have to escape it in the pattern. That is what the backslash does. You don't need to escape it inside of a character class, though, so your usage of it was OK.
LVL 13

Expert Comment

ID: 33717502
Ah I see. ok cool thanks.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question