File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regex validation for toll free number in USA. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex validation for toll free number in USA." Watch "Regex validation for toll free number in USA." New topic
Author

Regex validation for toll free number in USA.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Can any one suggest me regex for toll free number validation for USA?


Pawan Chopra
SCJP - DuMmIeS mInD
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
I don't know how refined you want it to be. I suggest that you should look at the description table in the java.util.regex.Pattern class under the section "Predefined character classes."

Then, for format, look at the Wikipedia page: Toll-free telephone number: North America
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

Can you tell us what you consider a toll free number in the U.S.A.? Where are you calling from? Do you need the country code prefix? do you need to dial '9' first to get an outside line? do you want to allow for the parens that often go around the area code?

What makes a number toll free? From where I sit, any number in the 314 area code is toll free (since that's where I live), but that may not be true for someone else.

My larger point is that the trick to defining the regex is to first understand what it is, EXACTLY, that you want to match. Tell us what, EXACTLY, you think a USA toll free number should look like.

Note that simply giving one example does NOT do that. giving ten examples does not do that. A thousand might, but would also probably still not be complete. think about corner and end cases - those are always the tricky one.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Thanks All!

Fred,

I will try to come up with better explanation on this and hopefully get help on that.
 
GeeCON Prague 2014
 
subject: Regex validation for toll free number in USA.