Telephone Format Validation Rule In Java

We need to write some server side validation in pure java code. 
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.. 

e.g. What if the phone number is like 1-800-555-9SUN 

Solution:

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? 

The last time I wrote a basic phone format validation class I simply stripped out a lot of those characters. 

For instance: 

number = number.replace(" ", ""); 
number = number.replace("-", ""); 
number = number.replace("(", ""); 
number = number.replace(")", ""); 
number = number.replace(" ", "");
number = number.replace("-", "");
number = number.replace("(", "");
number = number.replace(")", "");

After that all you need to do is check a) the length, and b) if it's all digits or not. 

You can first modify your entire String before validating. 

For instance: 

boolean isValidNumber(String number) 

    StringBuilder sb = new StringBuilder(number.length()); 
    for (int i = 0; i < number.length(); i++) 
    { 
        char c = number.charAt(i); 
        if ('0' <= c && c <= '9') 
        { 
            sb.append(c); 
        } 
        else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) 
        { 
            sb.append(mapLetterToDigit(c)); // e.g. mapLetterToDigit('p') returns 7 
        } 
        else if (c != '(' && c != ')' && c != '-' && !Character.isWhitespace(c)) // maybe other characters 
        { 
            return false; 
        } 
    } 
    return sb.length() > 0; 

boolean isValidNumber(String number)
{
    StringBuilder sb = new StringBuilder(number.length());
    for (int i = 0; i < number.length(); i++)
    {
        char c = number.charAt(i);
        if ('0' <= c && c <= '9')
        {
            sb.append(c);
        }
        else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
        {
            sb.append(mapLetterToDigit(c)); // e.g. mapLetterToDigit('p') returns 7
        }
        else if (c != '(' && c != ')' && c != '-' && !Character.isWhitespace(c)) // maybe other characters
        {
            return false;
        }
    }
    return sb.length() > 0;
}

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. 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.