my dog learned polymorphism*
The moose likes Java in General and the fly likes regular expression example telephone number validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regular expression example telephone number validation" Watch "regular expression example telephone number validation" New topic
Author

regular expression example telephone number validation

anagha pendse
Greenhorn

Joined: Feb 01, 2005
Posts: 6
I am looking for a regular expression example to validate the Phone number

Could someone point me to such an example using the RegEx in Java1.4 ?

Thanks in advance,
-anagha
Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Hello anagha,
I am hereby posting the regular expression for validating the telephone numbers.I hope it solves your purpose.

Expression: ^(? ?<1>[(])?(?<AreaCode>[2-9]\d{2})(?(1)[)])(?(1)(?<2>[ ])
|(? ?<3>[-])|(?<4>[ ])))?)?(?<Prefix>[1-9]\d{2})(?(AreaCode
)(? ?(1)(?(2)[- ]|[-]?))|(?(3)[-])|(?(4)[- ]))|[- ]?)(?<Suf
fix>\d{4})$

Description: Regular expression for validating US telephone numbers with OPTIONAL area code. Matches various permutations of formatting characters (parenthesis, space, dash). Parses the telephone number area code, prefix, and suffix to named groups to facilitate program manipulation. Area code is optional and can optionally be enclosed in parentheses. Rejects area codes that begin with 0 or 1 and prefixes that begin with 0. Rejects all telephone numbers that do not match on exactly 7 digits, or on exactly 10 digits with the optional area code, not counting the formatting characters.
Matches: [333-4444], [222 333 4444], [(222) 333-4444]
Non-Matches: [222333 4444], [222-333 4444], [(222)-333 4444]


Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Of course that's a completely invalid regexp to validate phone numbers in just about any other country...

Dutch numbers for example contain usually 7 digits and an optional 3 digit area code (the first number of which must be a zero), but there are exceptions where the number is 6 digits and a 4 digit area code.
But other numbers are possible. Take mobile phones which here are 10 digits, with the first two always being 06.
Or take 0800 and 090x numbers which can be as short as 4 digits (plus the 0800/090x) or as long as over a dozen.
Now if you want to allow historical numbers as well, it gets even more interesting (as our numbering system changed dramatically in the 1980s and again in the 1990s)

Add country codes and it gets more complex still.

And that's just one (and a rather small) country.


42
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
There are dozens of examples to choose from at http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
It also needs to be mentioned that that regex uses conditionals and named groups--two features not supported by the jdk1.4 regex engine.

And, of course, you have to turn smilies off if you want to post regexes.

BTW, don't regex questions belong in the Intermediate forum?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
There's also your other post with the same question in the Other Java APIs forum.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by David Harkness:
There's also your other post with the same question in the Other Java APIs forum.


Thanks for pointing that out. I closed the other post, with a link to this thread.
anagha pendse
Greenhorn

Joined: Feb 01, 2005
Posts: 6
Thanks Ranchers ,
That was really helpful.
Yes - in my haste to get answers I put the same question again at a different forum

My apologies
Once again thanks for all your imputs

--anagha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression example telephone number validation