• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Telephone format validation rule in java

 
Simpson Kumar
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Misha Ver
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sriram sankar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking number of digits is much easier, instead of worrrying about the format...
 
Misha Ver
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to be obtuse. If you want to include alphabetic characters, just do it.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic