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("修改失败"); } }
原文链接:https://blog.csdn.net/sinat_37239798/article/details/114120748?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167034653416782395339954%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=167034653416782395339954&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-28-114120748-null-null.nonecase&utm_term=%E6%89%8B%E6%9C%BA
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容