• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Telephone format validation rule in java

 
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
 
Ranch Hand
Posts: 470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
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
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic