Programmatically Validating US and Canada Phone Numbers Using Regular Expressions In Java

The following java program checks for the validity of US and Canada phone number formats using regular expression. The regular expression used here can handle the following rules:

  • The starting character may be ‘+’ but is optional
  • If the country code is used, it can be either 0 or 1 and is optional
  • Various codes (country, area) might be separated with ‘-‘ or ‘.’ but is optional
  • Area code should not start with 0 but may be optionally enclosed in round brackets
package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * Last Modified on 05/11/2011
 * Java Regular Expression to Validate US and Canada phone numbers.
 */
import java.util.regex.Pattern;

public class RegexUSAndCandaPhoneNumberValidator{
	/**
	 * REGEX IS: ^[+]?[01]?[- .]?(([2-9]d{2})|[2-9]d{2})[- .]?d{3}[- .]?d{4}$
	 * Escape Sequences are added in the following String for back slash ()
	 */
	static String phoneValidationUSCandaRegex =
		"^[+]?[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";

	/**
	 * This method compares the parameter string against the US Phone number
	 * regex and returns true if the pattern is matched - meaning the phone
	 * number is valid.
	 *
	 * The regular expression used here can handle the following rules:
	 * The starting character may be '+' but is optional
	 * If the country code is used, it can be either 0 or 1 and is optional
	 * Various codes (country, area) might be separated with '-' or '.' but is optional
	 * Area code should not start with 0 but may be optionally enclosed in round brackets
	 *
	 */
	public static boolean isAValidUSPhoneNumber(String str) {
		return Pattern.matches(phoneValidationUSCandaRegex, str);

	}

	public static void main (String [] args ) {
		String [] phoneNumbersToValidate={"2-309-778-4234",
										  "1-309-778-4423",
										  "309-778-4235",
										  "3097784234",
										  "309.778.4234",
										  "01-309-798-4234",
										  "001-309-798-4234",
										  "0-309-798-4234",
										  "+1-309-798-4235",
										  "1-3097980577",
										  "1-309.7980578",
										  "1-(309)-788-8978"};

		for(int index=0;index<phoneNumbersToValidate.length;index++) {
			String number=phoneNumbersToValidate[index];
			boolean isValid=isAValidUSPhoneNumber(phoneNumbersToValidate[index]);
			System.out.println(number+" : "+(isValid?"Valid":"Invalid"));
		}
	}
}

How to validate PO Box Address Variations With Java Regular Expressions?

Address validation in Java could be a little tricky thing if you are unwilling to use Java Regular Expression. This is because of complexity caused by the variations of the components of the address that people can use.

In this tutorial, we will focus on doing the PO Box validation in an address using regular expressions in Java. The tutorial contains test cases for variations of the PO Box formats.

package com.kushal.tools;
/**
 * @author Kushal Pauduyal
 * Created on: 05/11/2011
 * Last Modified On: 05/11/2011
 * 
 * This class contains method and unit tests to validate the PO Box in an address.
 * The regular expression can validate several different variations of PO Boxes.
 * This should pretty much satisfy all international PO Box numbers, if not the 
 * pattern can be modified to satisfy one.
 */
import java.util.regex.Pattern;

public class RegexPOBoxValidation {

	/**
	 * Patterns are greatest things in Java. The following pattern can identify the Post Box
	 */
	static final String thePattern = 
"([\w\s*\W]*(P(OST)?.?\s*((O(FF(ICE)?)?)?.?\s*(B(IN|OX|.?))|B(IN|OX))+))[\w\s*\W]*";	
	
	/**
	 * 
	 * @param str - PO Box string to validate
	 * @return if the input string is a valid PO Box format.
	 */
	public static boolean isValid(String str) {
		return Pattern.matches(thePattern, str);
	}

	public static void main (String [] args ) {
		/**
		 * Doing unit test with several different PO Box usage alterations.
		 */
		String [] itemsToValidate = {
				"PO Box",
				"P O Box",
				"P. O. Box",
				"P.O.Box",
				"Post Box",
				"Post Office Box",
				"Post Office",
				"P.O.B",
				"P.O.B.",
				"POB",
				"Post Office Bin",
				"Box",
				"Bin",
				"Post",
				"Postal Code",
				"100,, P O Box Des Moines",
				" P O Box DesMoines1000",
				" P O Box Des Moines 1000",
				" Post Office Box",
				" Post Office Box  ",
				"Post Box #"};										 

		for (int index = 0; index < itemsToValidate.length; index++) {
			String item = itemsToValidate[index];
			boolean isValid = isValid(itemsToValidate[index].toUpperCase());
			System.out.println(item + " : " + (isValid ? "Valid" : "Invalid"));
		}
	}
			
}


Here is the output of this java program:

PO Box : Valid
P O Box : Valid
P. O. Box : Valid
P.O.Box : Valid
Post Box : Valid
Post Office Box : Valid
Post Office : Invalid
P.O.B : Valid
P.O.B. : Valid
POB : Valid
Post Office Bin : Valid
Box : Invalid
Bin : Invalid
Post : Invalid
Postal Code : Invalid
100,, P O Box Des Moines : Valid
 P O Box DesMoines1000 : Valid
 P O Box Des Moines 1000 : Valid
 Post Office Box : Valid
 Post Office Box   : Valid
Post Box # : Valid