Input and validate data from user using Regular Expression - Java Regular Expressions

Java examples for Regular Expressions:Match

Description

Input and validate data from user using Regular Expression

Demo Code

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // get user input
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter first name:");
    String firstName = scanner.nextLine();
    System.out.println("Please enter last name:");
    String lastName = scanner.nextLine();
    System.out.println("Please enter address:");
    String address = scanner.nextLine();
    System.out.println("Please enter city:");
    String city = scanner.nextLine();
    System.out.println("Please enter state:");
    String state = scanner.nextLine();
    System.out.println("Please enter zip:");
    String zip = scanner.nextLine();
    System.out.println("Please enter phone:");
    String phone = scanner.nextLine();

    // validate user input and display error message
    System.out.println("\nValidate Result:");

    if (!validateFirstName(firstName))
      System.out.println("Invalid first name");
    else if (!validateLastName(lastName))
      System.out.println("Invalid last name");
    else if (!validateAddress(address))
      System.out.println("Invalid address");
    else if (!validateCity(city))
      System.out.println("Invalid city");
    else if (!validateState(state))
      System.out.println("Invalid state");
    else if (!validateZip(zip))
      System.out.println("Invalid zip code");
    else if (!validatePhone(phone))
      System.out.println("Invalid phone number");
    else/* w  w  w . ja  va  2  s  . co  m*/
      System.out.println("Valid input.  Thank you.");
  }

  // validate first name
  public static boolean validateFirstName(String firstName) {
    return firstName.matches("[A-Z][a-zA-Z]*");
  }

  // validate last name
  public static boolean validateLastName(String lastName) {
    return lastName.matches("[a-zA-z]+(['-][a-zA-Z]+)*");
  }

  // validate address
  public static boolean validateAddress(String address) {
    return address.matches("\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
  }

  // validate city
  public static boolean validateCity(String city) {
    return city.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
  }

  // validate state
  public static boolean validateState(String state) {
    return state.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
  }

  // validate zip
  public static boolean validateZip(String zip) {
    return zip.matches("\\d{5}");
  }

  // validate phone
  public static boolean validatePhone(String phone) {
    return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
  }
}

Result


Related Tutorials