By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

phone number regular expression problem

P: n/a
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

May 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
^(\+{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


May 31 '06 #2

P: n/a

<mr*********@googlemail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
^(\+{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

May 31 '06 #3

P: n/a
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: <mr*********@googlemail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
^(\+{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


Jun 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.