1. package com.tsu.util;
    2. import java.util.regex.Pattern;
    3. /**
    4. * @author di-zhou
    5. * @create 校验工具类
    6. */
    7. public class UtilValidate {
    8. /**
    9. * 正则表达式:验证用户名
    10. */
    11. public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
    12. /**
    13. * 正则表达式:验证密码
    14. */
    15. public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
    16. /**
    17. * 正则表达式:验证手机号
    18. */
    19. public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    20. /**
    21. * 正则表达式:验证邮箱
    22. */
    23. public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    24. /**
    25. * 正则表达式:验证汉字
    26. */
    27. public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    28. /**
    29. * 正则表达式:验证身份证
    30. */
    31. public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
    32. /**
    33. * 正则表达式:验证URL
    34. */
    35. public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    36. /**
    37. * 正则表达式:验证IP地址
    38. */
    39. public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
    40. /**
    41. * 校验用户名
    42. *
    43. * @param username
    44. * @return 校验通过返回true,否则返回false
    45. */
    46. public static boolean isUsername(String username) {
    47. return Pattern.matches(REGEX_USERNAME, username);
    48. }
    49. /**
    50. * 校验密码
    51. *
    52. * @param password
    53. * @return 校验通过返回true,否则返回false
    54. */
    55. public static boolean isPassword(String password) {
    56. return Pattern.matches(REGEX_PASSWORD, password);
    57. }
    58. /**
    59. * 校验手机号
    60. *
    61. * @param mobile
    62. * @return 校验通过返回true,否则返回false
    63. */
    64. public static boolean isMobile(String mobile) {
    65. return Pattern.matches(REGEX_MOBILE, mobile);
    66. }
    67. /**
    68. * 校验邮箱
    69. *
    70. * @param email
    71. * @return 校验通过返回true,否则返回false
    72. */
    73. public static boolean isEmail(String email) {
    74. return Pattern.matches(REGEX_EMAIL, email);
    75. }
    76. /**
    77. * 校验汉字
    78. *
    79. * @param chinese
    80. * @return 校验通过返回true,否则返回false
    81. */
    82. public static boolean isChinese(String chinese) {
    83. return Pattern.matches(REGEX_CHINESE, chinese);
    84. }
    85. /**
    86. * 校验身份证
    87. *
    88. * @param idCard
    89. * @return 校验通过返回true,否则返回false
    90. */
    91. public static boolean isIDCard(String idCard) {
    92. return Pattern.matches(REGEX_ID_CARD, idCard);
    93. }
    94. /**
    95. * 校验URL
    96. *
    97. * @param url
    98. * @return 校验通过返回true,否则返回false
    99. */
    100. public static boolean isUrl(String url) {
    101. return Pattern.matches(REGEX_URL, url);
    102. }
    103. /**
    104. * 校验IP地址
    105. *
    106. * @param ipAddr
    107. * @return
    108. */
    109. public static boolean isIPAddr(String ipAddr) {
    110. return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    111. }
    112. }