java校验电话联系方式

java后台校验联系方式使用到的两个类

java.util.regex.Pattern
java.util.regex.Matcher

封装的方法

/**
*phone :手机号
*return : true or false
*/
public boolean isMobile(String phone){
	Pattern p1 = null;
	Pattern p2 = null;
	Pattern p3 = null;
	Matcher m = null;
	boolean resutl = false;
	p1 = Pattern .compile("^[1][3,4,5,7,8][0-9]{9}$");//手机号
	p2 = Pattern .compile("^[0][1-9]{2,3}-[0-9]{5,10}$");//带区号验证
	p3 = Pattern .compile("^[1-9]{1}[0-9]{5,8}$");//没有区号的验证
	if(phone.length() > 11){
		m = p1.matcher(phone);
		result = m.matches();
	}else if(phone.length() > 9){
		m = p2.matcher(phone);
		result = m.matches();
	}else{
		m = p3.matcher(phone);
		result = m.matches();
	}
	return result;
}

你可能感兴趣的:(java校验电话联系方式)