|
@@ -7,91 +7,144 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
/**
|
|
|
* 常用验证类,如邮箱,手机格式验证
|
|
|
+ *
|
|
|
* @author pengdc
|
|
|
*/
|
|
|
public class CommonValidator {
|
|
|
|
|
|
- private final static String MOBILE_NO = "^((13[0-9])|(14[5|7|9])|(15[^4,\\D])|(166)|(18[0-9])|(17[0|1|3|5|6|7|8])|(19[8|9]))\\d{8}$";
|
|
|
+ private final static String MOBILE_NO = "^((13[0-9])|(14[5|7|9])|(15[^4,\\D])|(166)|(18[0-9])|(17[0|1|3|5|6|7|8])|(19[8|9]))\\d{8}$";
|
|
|
|
|
|
- private static final String TELEPHONE_NO = "((\\d{10})|(\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}"
|
|
|
- + "|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
|
|
|
+ private static final String TELEPHONE_NO = "((\\d{10})|(\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}"
|
|
|
+ + "|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
|
|
|
|
|
|
- private static final String EMAIL_ADDR = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
|
|
|
+ private static final String EMAIL_ADDR = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
|
|
|
|
|
|
- private static final String PWD_CHECK_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
|
|
|
+ private static final String PWD_CHECK_REGEX = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
|
|
|
|
|
|
- private static Pattern mobileNoPattern = Pattern.compile(MOBILE_NO);
|
|
|
- private static Pattern telephoneNoPattern = Pattern.compile(TELEPHONE_NO);
|
|
|
- private static Pattern emailAddrPattern = Pattern.compile(EMAIL_ADDR);
|
|
|
- private static Pattern pwdCheckPattern = Pattern.compile(PWD_CHECK_REGEX);
|
|
|
+ private static Pattern mobileNoPattern = Pattern.compile(MOBILE_NO);
|
|
|
+ private static Pattern telephoneNoPattern = Pattern.compile(TELEPHONE_NO);
|
|
|
+ private static Pattern emailAddrPattern = Pattern.compile(EMAIL_ADDR);
|
|
|
+ private static Pattern pwdCheckPattern = Pattern.compile(PWD_CHECK_REGEX);
|
|
|
|
|
|
- /**
|
|
|
- * 是否邮箱地址
|
|
|
- * @param emailAddr
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean isEmailAddr(String emailAddr) {
|
|
|
- if (StringUtils.isBlank(emailAddr)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Matcher matcher = emailAddrPattern.matcher(emailAddr);
|
|
|
- return matcher.matches();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 是否邮箱地址
|
|
|
+ *
|
|
|
+ * @param emailAddr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isEmailAddr(String emailAddr) {
|
|
|
+ if (StringUtils.isBlank(emailAddr)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Matcher matcher = emailAddrPattern.matcher(emailAddr);
|
|
|
+ return matcher.matches();
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 是否手机
|
|
|
- * @param mobileNo
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public static boolean isMobileNo(String mobileNo) {
|
|
|
- if (StringUtils.isBlank(mobileNo)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Matcher matcher = mobileNoPattern.matcher(mobileNo);
|
|
|
- return matcher.matches();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 是否手机
|
|
|
+ *
|
|
|
+ * @param mobileNo
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean isMobileNo(String mobileNo) {
|
|
|
+ if (StringUtils.isBlank(mobileNo)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Matcher matcher = mobileNoPattern.matcher(mobileNo);
|
|
|
+ return matcher.matches();
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 是否座机
|
|
|
- * @param telephoneNo
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public static boolean isTelephoneNo(String telephoneNo) {
|
|
|
- if (StringUtils.isBlank(telephoneNo)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Matcher matcher = telephoneNoPattern.matcher(telephoneNo);
|
|
|
- return matcher.matches();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 是否座机
|
|
|
+ *
|
|
|
+ * @param telephoneNo
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean isTelephoneNo(String telephoneNo) {
|
|
|
+ if (StringUtils.isBlank(telephoneNo)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Matcher matcher = telephoneNoPattern.matcher(telephoneNo);
|
|
|
+ return matcher.matches();
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 是否是座机或手机号码
|
|
|
- * @param teleOrMobileNo
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public static boolean isTelephoneOrMobileNo(String teleOrMobileNo) {
|
|
|
- if (StringUtils.isBlank(teleOrMobileNo)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Matcher matcher1 = telephoneNoPattern.matcher(teleOrMobileNo);
|
|
|
- if (matcher1.matches()) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- Matcher matcher2 = mobileNoPattern.matcher(teleOrMobileNo);
|
|
|
- return matcher2.matches();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 是否是座机或手机号码
|
|
|
+ *
|
|
|
+ * @param teleOrMobileNo
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean isTelephoneOrMobileNo(String teleOrMobileNo) {
|
|
|
+ if (StringUtils.isBlank(teleOrMobileNo)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Matcher matcher1 = telephoneNoPattern.matcher(teleOrMobileNo);
|
|
|
+ if (matcher1.matches()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Matcher matcher2 = mobileNoPattern.matcher(teleOrMobileNo);
|
|
|
+ return matcher2.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否至少包含数字、字母,长度在6-16位的字符串
|
|
|
+ *
|
|
|
+ * @param password
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean isComplexityOfPwd(String password) {
|
|
|
+ if (StringUtils.isBlank(password)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Matcher matcher = pwdCheckPattern.matcher(password);
|
|
|
+ return matcher.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验银行卡号方法
|
|
|
+ *
|
|
|
+ * @param bankCard
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isBankCard(String bankCard) {
|
|
|
+ if (bankCard.length() < 15 || bankCard.length() > 19) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
|
|
|
+ if (bit == 'N') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return bankCard.charAt(bankCard.length() - 1) == bit;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
|
|
|
+ *
|
|
|
+ * @param nonCheckCodeBankCard
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
|
|
|
+ if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
|
|
|
+ || !nonCheckCodeBankCard.matches("\\d+")) {
|
|
|
+ //如果传的不是数据返回N
|
|
|
+ return 'N';
|
|
|
+ }
|
|
|
+ char[] chs = nonCheckCodeBankCard.trim().toCharArray();
|
|
|
+ int luhmSum = 0;
|
|
|
+ for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
|
|
|
+ int k = chs[i] - '0';
|
|
|
+ if (j % 2 == 0) {
|
|
|
+ k *= 2;
|
|
|
+ k = k / 10 + k % 10;
|
|
|
+ }
|
|
|
+ luhmSum += k;
|
|
|
+ }
|
|
|
+ return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
|
- /**
|
|
|
- * 是否至少包含数字、字母,长度在6-16位的字符串
|
|
|
- * @param telephoneNo
|
|
|
- * @return boolean
|
|
|
- */
|
|
|
- public static boolean isComplexityOfPwd(String password) {
|
|
|
- if (StringUtils.isBlank(password)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- Matcher matcher = pwdCheckPattern.matcher(password);
|
|
|
- return matcher.matches();
|
|
|
}
|
|
|
|
|
|
}
|