常用正則校驗
public class RegexUtils {
/**
* 手機號正則匹配
*
* @param mobileNumber
* 手機號
* @return true:手機號格式正確
*/
public static final boolean mobileRegex(String mobileNumber) {
// 非空判斷
if (null == mobileNumber)
return false;
Pattern p = Pattern.compile("^1[34578]\\d{9}$");
Matcher m = p.matcher(mobileNumber);
return m.matches();
}
/**
* 簡單手機號正則匹配(11位數字)
*
* @param mobile
* 手機號
* @return true:手機號格式正確
*/
public static final boolean simpleMobileRegex(String mobile) {
// 非空判斷
if (null == mobile)
return false;
Pattern p = Pattern.compile("^[0-9]\\d{10}$");
Matcher m = p.matcher(mobile);
return m.matches();
}
/**
* 郵箱地址正則匹配
*
* @param email
* 郵箱地址
* @return true:郵箱地址格式正確
*/
public static final boolean emailRegex(String email) {
// 非空判斷
if (null == email)
return false;
Pattern p = Pattern
.compile("^\\[email protected][a-z0-9A-Z]+\\.[a-z]+$");
Matcher m = p.matcher(email);
return m.matches();
}
/**
* 身份證號碼正則匹配
*
* @param idCardNo
* 身份證號碼
* @return true:身份證號碼格式正確
*/
public static final boolean idCardNoRegex(String idCardNo) {
// 非空判斷
if (null == idCardNo)
return false;
Pattern p = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
Matcher m = p.matcher(idCardNo);
return m.matches();
}
/**
* 簡單銀行卡號正則匹配(六至十九位數字)
*
* @param bankCardNo
* 銀行卡號
* @return true:銀行卡號
*/
public static final boolean bankCardNoRegex(String bankCardNo) {
// 非空判斷
if (null == bankCardNo)
return false;
Pattern p = Pattern.compile("(^\\d{6,19}$)");
Matcher m = p.matcher(bankCardNo);
return m.matches();
}
/**
* 密碼複雜度正則
*
* @param pwd
* @return true:符合要求
*/
public static final boolean pwdComplexRegex(String pwd) {
if (StringUtils.isBlank(pwd))
return false;
// 密碼長度校驗
int pwdLen = pwd.length();
if (pwdLen < 6 || pwdLen > 20)
return false;
// 6-20位數字,字母或符號
Pattern p = Pattern.compile("^.{6,20}$");
Matcher m = p.matcher(pwd);
return m.matches();
}
/**
* 身份證格式匹配
*
* @param identityCardNo
* @return true:居民身份證格式正確
*/
public static final String identityCardNoRegex(String cardType, String identityCardNo) {
String message = null;
// 居民身份證
if (CardType.ID_CARD.getIndex().equals(cardType)) {
Pattern p = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
Matcher m = p.matcher(identityCardNo);
if (!m.matches()) {
message = "居民身份證格式錯誤!";
return message;
}
}
// 護照
if (CardType.PASSPORT.getIndex().equals(cardType)) {
Pattern p = Pattern.compile("^[a-zA-Z]{5,17}$|^[a-zA-Z0-9]{5,17}$");
Matcher m = p.matcher(identityCardNo);
if (!m.matches()) {
message = "護照格式錯誤!";
return message;
}
}
// 工商註冊號碼
if (CardType.BUSINESS_REGISTRATION_CODE.getIndex().equals(cardType)) {
Pattern p = Pattern.compile("[^_IOZSVa-z\\W]{2}\\d{6}[^_IOZSVa-z\\W]{10}$");
Matcher m = p.matcher(identityCardNo);
if (!m.matches()) {
message = "工商註冊號碼格式錯誤!";
return message;
}
}
// 組織機構程式碼
if (CardType.ORGANIZATION_CODE.getIndex().equals(cardType)) {
Pattern p = Pattern.compile("[a-zA-Z0-9]{8}-[a-zA-Z0-9]");
Matcher m = p.matcher(identityCardNo);
if (!m.matches()) {
message = "組織機構程式碼格式錯誤!";
return message;
}
}
// 軍官證
if (CardType.MILITARY_CARD.getIndex().equals(cardType)) {
Pattern p = Pattern.compile("\\d{6,8}$");
Matcher m = p.matcher(identityCardNo);
if (!m.matches()) {
message = "軍人證格式錯誤(6到8位數字)!";
return message;
}
}
return message;
}
/**
* 實名姓名正則
*
* @param realName
* @return true:真實姓名吻合
*/
public static final boolean realNameRegex(String realName) {
if (StringUtils.isBlank(realName))
return false;
Pattern p = Pattern.compile("^(([\u4E00-\u9FA5]{2,50})|([a-zA-Z]{3,50}))$");
Matcher m = p.matcher(realName);
return m.matches();
}
/**
* 圖片型別正則
*
* @param 圖片型別
* @return true:圖片型別吻合
*/
public static final boolean fileTypeRegex(String fileType) {
Pattern p = Pattern.compile("^(png|jpg|bmp|gif|PNG|JPG|BMP|GIF)$");
Matcher m = p.matcher(fileType);
return m.matches();
}
/**
* 電話QQ正則
*
* @param 電話QQ
* @return true:電話QQ吻合
*/
public static final boolean QQMobilePhoneRegex(String number) {
Pattern p = Pattern.compile("^[0-9]*$");
Matcher m = p.matcher(number);
return m.matches();
}
}