正则表达式校验手机号码
1.首先了解下目前手机号码的规则/**运营商号段如下:中国联通号码:130、131、132、145(无线上网卡)、155、156、185(iPhone5上市后开放)、186、176(4G号段)、175(2015年9月10日正式启用,暂只对北京、上海和广东投放办理)中国移动号码:134、135、136、137、138、139、147(无线上网卡)、150、151、152、157、158、159、18
·
1.首先了解下目前手机号码的规则
/**
- 运营商号段如下:
- 中国联通号码:130、131、132、145(无线上网卡)、155、156、185(iPhone5上市后开放)、186、176(4G号段)、
- 175(2015年9月10日正式启用,暂只对北京、上海和广东投放办理)
- 中国移动号码:134、135、136、137、138、139、147(无线上网卡)、150、151、152、157、158、159、182、183、187、188、178
- 中国电信号码:133、153、180、181、189、177、173、149 虚拟运营商:170、1718、1719
- 手机号前3位的数字包括:
- 1 :1
- 2 :3,4,5,7,8
- 3 :0,1,2,3,4,5,6,7,8,9
- 总结: 目前java手机号码正则表达式有:
- a :"^1[3|4|5|7|8][0-9]\d{4,8}$" 一般验证情况下这个就可以了
- b :"^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\d{8}$"
*/
2.再代码中的应用
- 工具类中验证代码:
//验证手机号
public static final String REGEX_MOBILE = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
/**
* 验证手机号是否合法
* @Author HB
* @param phone 手机号
* @return boolean true合法 false不合法
* @Date 2021/2/26 11:05
*
**/
public static boolean isPhone(String phone){
if (phone.matches(REGEX_MOBILE)){
return true;
}else {
return false;
}
}
- 控制类Controller中的应用
@PutMapping(value = "/update")
@ApiOperation(value = "更新荣誉人员")
public RestResult<String> update(@RequestBody DatagoHonorPerson honorPerson) {
//验证手机号代码
if (!Utils.isPhone(honorPerson.getPhone())){
honorPerson.setPhone(honorPerson.getPhone()+"-错误的手机号");
}
honorPerson.setUpdateTime(new Date());
int i = honorPersonService.updateByPrimaryKeySelective(honorPerson);
if (i > 0) {
return RestResultUtil.ok("修改成功");
} else {
return RestResultUtil.failed("修改失败");
}
}
更多推荐
所有评论(0)