is string a Phone number by regex - Java java.util.regex

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

Description

is string a Phone number by regex

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Pattern;

public class Main{

    public static void main(String[] argv){
        String phone = "java2s.com";
        System.out.println(isPhone(phone));
        phone = "21123123";
        System.out.println(isPhone(phone));
    }//from www .  ja va  2  s .co m
    private final static Pattern PHONE = Pattern.compile("^1\\d{10}$");

    public static boolean isPhone(String phone) {
        return !isEmpty(phone) && PHONE.matcher(phone).matches();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0
                || str.trim().equalsIgnoreCase("null");
    }

}

Related Tutorials