java正則表達(dá)式表單驗(yàn)證類工具類(驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等)
java使用正則表達(dá)式進(jìn)行表單驗(yàn)證工具類,可以驗(yàn)證郵箱、手機(jī)號(hào)碼、qq號(hào)碼等
package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 使用正則表達(dá)式進(jìn)行表單驗(yàn)證
*
*/
public class RegexValidateUtil {
static boolean flag = false;
static String regex = "";
public static boolean check(String str, String regex) {
try {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}
/**
* 驗(yàn)證非空
*
* @param email
* @return
*/
public static boolean checkNotEmputy(String notEmputy) {
regex = "^\\s*$";
return check(notEmputy, regex) ? false : true;
}
/**
* 驗(yàn)證郵箱
*
* @param email
* @return
*/
public static boolean checkEmail(String email) {
String regex = "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$ ";
return check(email, regex);
}
/**
* 驗(yàn)證手機(jī)號(hào)碼
*
* 移動(dòng)號(hào)碼段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
* 聯(lián)通號(hào)碼段:130、131、132、136、185、186、145
* 電信號(hào)碼段:133、153、180、189
*
* @param cellphone
* @return
*/
public static boolean checkCellphone(String cellphone) {
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
return check(cellphone, regex);
}
/**
* 驗(yàn)證固話號(hào)碼
*
* @param telephone
* @return
*/
public static boolean checkTelephone(String telephone) {
String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
return check(telephone, regex);
}
/**
* 驗(yàn)證傳真號(hào)碼
*
* @param fax
* @return
*/
public static boolean checkFax(String fax) {
String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
return check(fax, regex);
}
/**
* 驗(yàn)證QQ號(hào)碼
*
* @param QQ
* @return
*/
public static boolean checkQQ(String QQ) {
String regex = "^[1-9][0-9]{4,} $";
return check(QQ, regex);
}
}
相關(guān)文章
Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案
大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案,感興趣的朋友跟隨小編一起看看吧2024-04-04
SpringBoot實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能
在Spring Boot項(xiàng)目中實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能,可以通過使用Spring MVC的MultipartFile接口來處理文件上傳,并使用HttpServletResponse或Resource來實(shí)現(xiàn)文件下載和預(yù)覽,下面是如何實(shí)現(xiàn)這些功能的完整示例,需要的朋友可以參考下2024-08-08
idea數(shù)據(jù)庫驅(qū)動(dòng)下載失敗的問題及解決
這篇文章主要介紹了idea數(shù)據(jù)庫驅(qū)動(dòng)下載失敗的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
java使用正則表達(dá)為數(shù)字添加千位符的簡(jiǎn)單方法
這篇文章主要介紹了java使用正則表達(dá)為數(shù)字添加千位符的簡(jiǎn)單方法,需要的朋友可以參考下2014-04-04
Springboot如何設(shè)置靜態(tài)資源緩存一年
這篇文章主要介紹了Springboot如何設(shè)置靜態(tài)資源緩存一年,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Java 數(shù)據(jù)庫時(shí)間返回前端顯示錯(cuò)誤(差8個(gè)小時(shí))的解決方法
本文主要介紹了Java 數(shù)據(jù)庫時(shí)間返回前端顯示錯(cuò)誤(差8個(gè)小時(shí))的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08

