Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Telephone format validation rule in java

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular expressions
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to be obtuse. If you want to include alphabetic characters, just do it.
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic