• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regular expression example telephone number validation

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are dozens of examples to choose from at http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's also your other post with the same question in the Other Java APIs forum.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic