Stanton_Roux
asked on
Phone number Regular Expression Validators
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
0845678890
I need them to write the number as
+27845678890
I currently have a kasked extender that works like this
<ajaxToolkit:MaskedEditExt ender runat="server" ID="mee" ClearMaskOnLostFocus="fals e" TargetControlID="txtCell" Mask="+27999999999"></ajax Toolkit:Ma skedEditEx tender>
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 +
Thanks
Stanton
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
0845678890
I need them to write the number as
+27845678890
I currently have a kasked extender that works like this
<ajaxToolkit:MaskedEditExt
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 +
Thanks
Stanton
you can use the asp:regular expression control.
Here is the regex you are looking for:
"^[+][2][7][1-9]{8}$"
CPG
Here is the regex you are looking for:
"^[+][2][7][1-9]{8}$"
CPG
You can try this
\+27[0-9]{9}
\+27[0-9]{9}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
<?php
$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;
?>
Using php you can do this way:
<?php
$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;
?>
I misread your question, I thought you wanted 11 including the + not excluding..
Modification of my previous post:
"^[+][2][7][1-9]{9}$"
Modification of my previous post:
"^[+][2][7][1-9]{9}$"
@copyPasteGhost
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:
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:
^\+27[1-9]\d{8}$
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?
thanks.
CPG
what does
^\+27 mean?
I know ^ is the beginning of the line.
but what does the \ before "+27" do?
thanks.
CPG
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.
Ah I see. ok cool thanks.
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.
Jon