DefaultMobileCheck.java 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.yihu.base.security.sms.mobile;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. * Created by chenweida on 2017/12/8.
  6. */
  7. public class DefaultMobileCheck implements MobileCheck {
  8. @Override
  9. public Boolean checkMobile(String mobile) {
  10. return checkCellphone(mobile);
  11. }
  12. /**
  13. * 验证手机号码
  14. * <p>
  15. * 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
  16. * 联通号码段:130、131、132、136、185、186、145
  17. * 电信号码段:133、153、180、189
  18. *
  19. * @param cellphone
  20. * @return
  21. */
  22. public static boolean checkCellphone(String cellphone) {
  23. String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
  24. return check(cellphone, regex);
  25. }
  26. /**
  27. * 使用正则表达式进行表单验证
  28. */
  29. public static boolean check(String str, String regex) {
  30. boolean flag = false;
  31. try {
  32. Pattern pattern = Pattern.compile(regex);
  33. Matcher matcher = pattern.matcher(str);
  34. flag = matcher.matches();
  35. } catch (Exception e) {
  36. flag = false;
  37. }
  38. return flag;
  39. }
  40. }