Determine if the given address is a valid phone number using regex - Java java.util.regex

Java examples for java.util.regex:Match Phone Number

Description

Determine if the given address is a valid phone number using regex

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String address = "java2s.com";
        System.out.println(isPhone(address));
    }//from   w w w  .  ja va  2s  . c  o  m

    private final static String REG_EX_PHONE = "(\\+?[1]-?)?[0-9]{3}-?[0-9]{3}-?[0-9]{4}";

    /**
     * Determine if the given address is a valid phone number
     * @param address the address to validate
     * @return {Boolean} whether or not address is a valid phone number
     * @method isPhone
     * @static
     */
    public static boolean isPhone(String address) {
        return address != null && address.matches(REG_EX_PHONE);
    }
}

Related Tutorials