aspose file tools*
The moose likes Java in General and the fly likes Telephone format validation rule in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Telephone format validation rule in java" Watch "Telephone format validation rule in java" New topic
Author

Telephone format validation rule in java

Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 261
we need to write some server side validation in pure java code, here i'm struggling with telephone validaion.
Our requirement is that we are getting the validation format from UI side dynamically and we need to validation the value according to that format.
Ex: if the format comes xxx-xxx-xxxx, we need to validate on that
if the format comes (xxx)xxx-xxxx, we need to validate on that, etc..

Please someone help on this to validate the telephone number on dynamic fromat


Thanks,
Kumar
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Regular expressions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Why not just ignore punctuation and count digits? As long as you get the correct number of digits, why should you care what the punctuation was?

Make things easy for your users and they'll love you forever.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

I agree with Bear. The last time I wrote a basic phone format validation class I simply stripped out a lot of those characters. For instance:
After that all you need to do is check a) the length, and b) if it's all digits or not.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sriram sankar
Ranch Hand

Joined: Feb 26, 2008
Posts: 43
Checking number of digits is much easier, instead of worrrying about the format...


SCJP 1.5 , SCBCD 5.0, SCEA 5.0
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
sriram sankar wrote:Checking number of digits is much easier, instead of worrrying about the format...


What if the phone number is like 1-800-555-9SUN
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

No need to be obtuse. If you want to include alphabetic characters, just do it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

As said before, you can first modify your entire String before validating. For instance:
After the loop all whitespace characters, -, ( and ) are removed. All letters from A-Z (both upper and lower) are converted to numbers.

Of course regular expressions are also possible, but the regex would be quite hard to accommodate all formats you want to allow; xxx-xxx-xxxx, (xxx)xxx-xxxx, xxx xxx xxxx, (xxx) xxx xxxx, xxxxxxxxxx, etc.

Come to think of it, if those are the only formats then it's actually not that hard:
- (xxx) or xxx; a closing bracket should only be allowed if there is an opening bracket
- whitespace, dash or nothing
- xxx
- whitespace, dash or nothing
- xxxx

Of course that regex would have to be applied after the letter-to-number mapping to allow 800-555-SUN1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Telephone format validation rule in java