package Cnkcomksskkutilkutils; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; /** * Commonly used some validation, such as mobile phones, mobile number, number Unicom, Telecom number, password, landline, zip code, email, age, ID, URL, QQ, Chinese characters, letters, numbers, etc. * / Public class ValidateUtil { / ** phone number of the rule * / public static Final String MOBILE_PATTERN = "^ ((13 [0-9]) | (14 [0-9]) | (15 [0-9]) | (17 [0-9 ]) | (18 is [0-9])) (. 8 \\ {D}) $ " ; / ** China Mobile telecommunication number format validation segment: 133,153,180,181,189,177,1700,173 * * / Private static Final String CHINA_TELECOM_PATTERN =" ( ?: ^ (:? \\ + 86) 1? (:? 33 | 53 | 7 [37] | 8 [019]) \\ d {8} $) | (: ^ (: \\ + 86?? ?) 1700 \\ d {7} $) " ; / ** China Unicom's mobile phone number format validation segment: 130,131,132,155,156,185,186,145,176,1707,1708,1709,175 * * / Private static Final String CHINA_UNICOM_PATTERN =" (: ^ (?:? \\ + 86) 1 (:? ? 3 [0-2] | 4 [5] | 5 [56] | 7 [56] | 8 [56]) \\ d {8} $) | (: ^? (:? \\ + 86) 170. [7-9] \\ {D} $. 7) "? ; / ** Chinese mobile phone number format validation segment: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 * * / Private static Final String CHINA_MOVE_PATTERN = "(: ^ (: \\ + 86) 1 (:???? 3 [4-9] | 4 [ 7] | 5 [0-27-9] | 7 [8] | 8 [2-478]) \\ d {8} $) | (:??? ^ (: \\ + 86) 1705 \\ d {7} $) " ; / ** password rules (6-16 letters, numbers) * / public static Final String PASSWORD_PATTERN =" ^ [0-9A-Za-Z] {6,16} $ " ; / * * solid number (landline) rule * / public static Final String LANDLINE_PATTERN = "^ (:? \\ ({3,4-} \\ \\ D) | D {3,4-} \\ -)? \\ {D } 7, 8? (: -? D \\ {l, 4}) $ " ; / ** ZIP code rule * / public static Final String POSTCODE_PATTERN = "[1-9] \\ {D}. 5" ; / ** mailbox rule * / public static Final String EMAIL_PATTERN = "^ ([the Z-A-z0-9A] + [- | _ | .? \\]) + [a -z0-9A-Z] @ ([a-z0-9A-Z] + (- [a-z0-9A-Z] +) \\) + [a-?. the Z-zA] {2,} $ " ; / ** Age rule between 1-120 * / public static Final String AGE_PATTERN =" ^ (:?? [1-9] [0-9] |. 1 [01] [0-9] | 120) $ " ; / ** ID rule * / public static Final String IDCARD_PATTERN =" \\ D ^ {15} | \\ {D} $ 18 is " ; / ** the URL rule, http , the WWW, the FTP * / public static Final String url_pattern = "HTTP (S):? // (. [\\ W-] + \\) + [\\ W-] + (/ [\\ W- ./?%&=]*)? " ; / ** QQ rule * / public static Final String QQ_PATTERN =" ^ [1-9] [0-9] {4,13} $ " ; / ** full Chinese rule * / public static Final String = CHINESE_PATTERN" ^ [\ u4E00- \ u9FA5] + $ " ; / ** full alphanumeric rule * / public static Final String STR_ENG_PATTERN =" ^ [A-Za-Z] + $ " ; / ** integer rule * / public static Final String = INTEGER_PATTERN "^ - [0-9] + $?" ; /**Positive integer rule * / public static Final String POSITIVE_INTEGER_PATTERN = "? ^ \\ + [1-9] [0-9] * $" ; /** * @Description: verify the phone number format * @Param Mobile phone number * @return boolean */ public static boolean validateMobile(String mobile) { if (StringUtils.isEmpty(mobile)){ return Boolean.FALSE; } return mobile.matches(MOBILE_PATTERN); } /** * Verify that the phone number is telecommunications, 133,153,180,189,177 * @Param Mobile phone number * @return boolean */ public static boolean validateTelecom(String mobile){ if(StringUtils.isEmpty(mobile)){ return Boolean.FALSE ; } return mobile.matches(CHINA_TELECOM_PATTERN) ; } /** * Verify that China Unicom mobile phone number 130,131,132,155,156,185,186,145,176,1707,1708,1709,175 * @Param Mobile phone number * @return boolean */ public static boolean validateUnionMobile(String mobile){ if(StringUtils.isEmpty(mobile)){ return Boolean.FALSE ; } return mobile.matches(CHINA_UNICOM_PATTERN) ; } /** * Verify that the mobile phone number * @Param Mobile phone number 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 * @return boolean */ public static boolean validateMoveMobile(String mobile){ if(StringUtils.isEmpty(mobile)){ return Boolean.FALSE ; } return mobile.matches(CHINA_MOVE_PATTERN) ; } /** * @Description: Verify password format 6-16 letters, numbers * @Param pwd password * @return boolean */ public static boolean validatePwd(String pwd) { if (StringUtils.isEmpty(pwd)){ return Boolean.FALSE; } return Pattern.matches(PASSWORD_PATTERN, pwd); } /** * Verify landline number, formats such as: 58654567,023-58654567 * @Param landline telephone, landline * @return boolean */ public static boolean validateLandLine(final String landline) { if(StringUtils.isEmpty(landline)){ return Boolean.FALSE; } return landline.matches(LANDLINE_PATTERN); } /** * Verify zip code * @Param postCode zip code * @return boolean */ public static boolean validatePostCode(final String postCode){ if(StringUtils.isEmpty(postCode)){ return Boolean.FALSE ; } return postCode.matches(POSTCODE_PATTERN) ; } /** * Verify mail (e-mail) * @Param Email mail (e-mail) * @return boolean */ public static boolean validateEamil(final String email){ if(StringUtils.isEmpty(email)){ return Boolean.FALSE ; } return email.matches(EMAIL_PATTERN) ; } /** * Judge between age, 1-120 * @Param Age Age * @return boolean */ public static boolean validateAge(final String age){ if(StringUtils.isEmpty(age)){ return Boolean.FALSE ; } return age.matches(AGE_PATTERN) ; } /** * ID card verification * @Param idCard ID * @return boolean */ public static boolean validateIDCard(final String idCard){ if(StringUtils.isEmpty(idCard)){ return Boolean.FALSE ; } return idCard.matches(IDCARD_PATTERN) ; } /** * URL address verification * @Param url the URL of address * @return boolean */ public static boolean validateUrl(final String url){ if(StringUtils.isEmpty(url)){ return Boolean.FALSE ; } return url.matches(URL_PATTERN) ; } /** * Verify QQ No. * @Param QQ QQ No. * @return boolean */ public static boolean validateQq(final String qq){ if(StringUtils.isEmpty(qq)){ return Boolean.FALSE ; } return qq.matches(QQ_PATTERN) ; } /** * Verify whether all Chinese character strings * @Param str String * @return boolean */ public static boolean validateChinese(final String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(CHINESE_PATTERN) ; } /** * Determine whether the whole string of letters * @Param str String * @return boolean */ public static boolean validateStrEnglish(final String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(STR_ENG_PATTERN) ; } /** * Determines whether an integer, including negative numbers * @Param str String * @return boolean */ public static boolean validateInteger(final String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(INTEGER_PATTERN) ; } /** * Is determined whether a positive integer greater than 0 * @Param str String * @return boolean */ public static boolean validatePositiveInt(final String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(POSITIVE_INTEGER_PATTERN) ; } }