Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

us zip codes regular expression validation

In some tests, users need to test valid US zip codes by using regular expressions. Zip code validation may be needed when locating elements in the html tag or on the UI. This article explains how to validate US zip code regular expression pattern in Java code.  The most commonly used zip code pattern is:

Regular Expression Pattern: \b[0-9]{5}(?:-[0-9]{4})?\b

Explanation: (explanation syntax is from http://regex101.com)

Here is the Java source code to validate the US Zip Code regular expression

Step 1: create a java project as shown below, add a class "ZipCodeValidator.java", and add a JUnit test class "ZipCodeValidatorTest.java". 

Step 2: write the following code in the ZipCodeValidator.java class

package com.seleniummaster.regularexpression;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ZipCodeValidator {
  
  //note in this expression below, each back slash is an escape character, so 
  //the regular expression should be "\b[0-9]{5}(?:-[0-9]{4})?\b"
  final static String zipcodePattern="\\b[0-9]{5}(?:-[0-9]{4})?\\b";
  private static Pattern pattern;
  private static Matcher matcher;
  public ZipCodeValidator()
  {
    pattern=Pattern.compile(zipcodePattern);
  }

  public boolean validate (String ZipCode)
  {
    matcher=pattern.matcher(ZipCode);
    return matcher.matches();
  }

}

Step 3: write the following code in the ZipCodeValidatorTest.java class

 
package com.seleniummaster.regularexpression;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ZipCodeValidatorTest {

  private ZipCodeValidator zipCodeValidator;

  @Before
  public void setUp() throws Exception {
    zipCodeValidator = new ZipCodeValidator();
  }

  @After
  public void tearDown() throws Exception {
  }
  @Test
  public void ValidZipCodeTest1() {
    System.out.println("Zip Code 12345-6789 "+ zipCodeValidator.validate("12345-6789"));
    assertTrue(zipCodeValidator.validate("12345-6789"));

  }
  @Test
  public void ValidZipCodeTest2() {
    System.out.println("Zip Code 12345-4567 "+ zipCodeValidator.validate("12345-4567"));
    assertTrue(zipCodeValidator.validate("12345-4567"));

  }
  
  @Test
  public void InvalidZipCodeTest1() {
    System.out.println("Zip Code 12345-67890 "+ zipCodeValidator.validate("12345-67890"));
    assertTrue(zipCodeValidator.validate("12345-67890"));
  }
  @Test
  public void InvalidZipCodeTest2() {
    System.out.println("Zip Code 12345-abcd "+ zipCodeValidator.validate("12345-abcd"));
    assertTrue(zipCodeValidator.validate("12345-abcd"));
  }

}

Step 4: run the test as JUnit test and see the result in the console and JUnit console.  Two zip codes are valid and other two zip codes are not correct, so two tests failed in the assertion. 

Result on the console: 

Zip Code 12345-6789 true
Zip Code 12345-4567 true
Zip Code 12345-67890 false
Zip Code 12345-abcd false

JUnit Result