Java正则表达式校验手机号及是否大陆手机号

package ks;

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


/**
 *  
 * 
 * @author Hj 
 *
 */

public class test {

	
	
	public static void main(String[] args) {
		List <String> list=new ArrayList<String>();
		List <String> numbers=new  ArrayList<String>();
		numbers.add("138 1234 1234");
		numbers.add("13812345abc");
		numbers.add("13812345678");
		numbers.add("138 1234 5678");
		numbers.add("98765432101");
		for (int i=0;i<numbers.size();i++){
			String test=isMobiPhoneNum(numbers.get(i).replaceAll("\\s*", ""));
			if(list.contains(numbers.get(i).replaceAll("\\s*", ""))){
				System.out.println("此手机号已经被其他用户注册");
			}else if("1".equals(test)){
				System.out.println("通过此手机号注册成功");
				list.add(numbers.get(i).replaceAll("\\s*", ""));
			}else if ("2".equals(test)){
				System.out.println("此手机号码为中国大陆非法手机号码");
			}else{
				System.out.println("手机号无法注册,为非法手机号");
			}
		}	
	}
	public static String isMobiPhoneNum(String telNum){
		String regex = "^\\d{0,11}$";
		String regexCh = "^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$";
		Pattern pCh = Pattern.compile(regexCh,Pattern.CASE_INSENSITIVE);
	    Matcher mCh = pCh.matcher(telNum);
        Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(telNum);
        boolean a=m.matches();
        boolean b=mCh.matches();
        if(m.matches()&&mCh.matches()){ //通过
        	return "1";
        }else if(m.matches()&&!mCh.matches()){ //非大陆
        	return "2";
        }else{ //不通过
        	return "3";
        }
	}

}

运行结果为:

猜你喜欢

转载自blog.csdn.net/jungeCSND/article/details/102900829