473,418 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

phone number regular expression problem

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
3 6739
^(\+{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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
1
by: Erik Jälevik | last post by:
I have a regular expression which I believe is correct but it's not behaving as expected. It is as follows: /\+?\d+*\d+/ The idea is that a phone number is allowed to be of the form: optional...
0
by: Brian Davis | last post by:
The problem is in the word boundary \b. A leading "(" will match as a word boundary before it gets to the test for a "(". Changing the expression to: (?n)(\b|\()1??\(?(?<areaCode>\d\d)?\)??(?...
2
by: Ori | last post by:
Hi, I'm looking for a good way to validate a US phone number and i though using regular expression for this. I want to support 3 different ways to enter a phone number: 1.Local Phone : 888-8899...
1
by: venu | last post by:
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...
2
by: venu | last post by:
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...
2
by: David C | last post by:
Is there a way to validate a specific phone# format on a control? I also want to be able to have the user enter an extension as part of the text. For example, the following would be valid. ...
1
by: hellboss | last post by:
Hi ! Can u tel me what is the expression (Regular expression) for the a field like Telephone number which Doesnt exceeds 6 digits Ex:999999 d{5} or ()* Can we use this expression , if...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.