phone number regular expression problem

  • Thread starter Thread starter venu
  • Start date Start date
V

venu

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
 
^(\+{0,1}[0-9\-]*)

seems to work for me, I tried it with:

+44-444-2342342

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
);
}
 
Yours caters for "ext" and various others, his rules are fairly plain:

^(\+{0,1}[0-9\-]*) works fine
^(\+{0,1}[0-9\-]*)

seems to work for me, I tried it with:

+44-444-2342342

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
);
}
 
Back
Top