Re: phone number regular expression problem
- From: "mrshrinkray@xxxxxxxxxxxxxx" <mrshrinkray@xxxxxxxxxxxxxx>
- Date: 1 Jun 2006 01:31:46 -0700
Yours caters for "ext" and various others, his rules are fairly plain:
The constraints are :
***********************
# It should accept any number of numbers
# any number of - hyphens
# and one + symbol
# no other characters and alphabets are allowed
^(\+{0,1}[0-9\-]*) works fine
Mythran wrote:
<mrshrinkray@xxxxxxxxxxxxxx> wrote in message
news:1149083237.738040.303340@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
^(\+{0,1}[0-9\-]*)
seems to work for me, I tried it with:
+44-444-2342342
venu wrote:
Hi,
I have a different requirement and it is :
I need to validate a phone number field.
It may or may not be a US phone number.
The constraints are :
***********************
# It should accept any number of numbers
# any number of - hyphens
# and one + symbol
# no other characters and alphabets are allowed
Thanks in advance
Venugopal.S
There are some awesome phone number examples out there....I am sure you can
find one that matches most phone numbers in most regions :)...but darn, that
has to be a HUGE pattern :P
Here's one that does U.S. and regional telephone numbers ... including
extentions (copied and slightly modified from
http://javascript.about.com/library/blre.htm).
----
/// <summary>
/// Determines whether the specified text is a valid phone number.
/// </summary>
/// <param name="Text">The text to check.</param>
/// <returns>
/// Returns <see langword="true" /> if the <paramref name="Text" />
/// argument is a valid phone number, otherwise returns <see
/// langword="false" />.
/// </returns>
private static bool IsPhoneNumber(string Text)
{
const string PHONE_PATTERN =
@"^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))" +
@"(-| )?(\d{3,4})(-| )?(\d{4})(\s*(x|ext)\d{1,5}){0,1}$";
return System.Text.RegularExpressions.Regex.IsMatch(
Text,
PHONE_PATTERN
);
}
----
HTH,
Mythran
.
- Prev by Date: Re: Can anyone tell me of any optimations I could do to this to make it faster?
- Next by Date: Detect orphan files after program crash
- Previous by thread: Re: A question about Exception
- Next by thread: Detect orphan files after program crash
- Index(es):
Relevant Pages
|