项目中经常遇到手机校验的正则,百度了很多,但是每次百度都要找半天,现在写下来以后直接就可以用
/**
* 手机号校验
* @param mobiles
* @return
*/
public static boolean isMobile(String mobiles){
String args=
"^1[34578]\\d{9}$";
Pattern p = Pattern.
compile(args)
;
Matcher m = p.matcher(mobiles)
;
return m.matches()
;
}
/**
* 固话校验
* @param fixedPhone
* @return
*/
public static boolean isFixedPhone(String fixedPhone){
String reg=
"(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+" +
"(\\-[0-9]{1,4})?)|" +
"(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+" +
"(\\-[0-9]{1,4})?)";
return Pattern.
matches(reg
, fixedPhone)
;
}