java正則表達(dá)式表單驗證類工具類(驗證郵箱、手機(jī)號碼、qq號碼等)
java使用正則表達(dá)式進(jìn)行表單驗證工具類,可以驗證郵箱、手機(jī)號碼、qq號碼等
package util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 使用正則表達(dá)式進(jì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;
}
/**
* 驗證非空
*
* @param email
* @return
*/
public static boolean checkNotEmputy(String notEmputy) {
regex = "^\\s*$";
return check(notEmputy, regex) ? false : true;
}
/**
* 驗證郵箱
*
* @param email
* @return
*/
public static boolean checkEmail(String email) {
String regex = "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$ ";
return check(email, regex);
}
/**
* 驗證手機(jī)號碼
*
* 移動號碼段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
* 聯(lián)通號碼段:130、131、132、136、185、186、145
* 電信號碼段: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);
}
/**
* 驗證固話號碼
*
* @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);
}
/**
* 驗證傳真號碼
*
* @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);
}
/**
* 驗證QQ號碼
*
* @param QQ
* @return
*/
public static boolean checkQQ(String QQ) {
String regex = "^[1-9][0-9]{4,} $";
return check(QQ, regex);
}
}
相關(guān)文章
基于java TCP網(wǎng)絡(luò)通信的實例詳解
本篇文章是對java中TCP網(wǎng)絡(luò)通信的實例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05SpringBoot Session共享實現(xiàn)圖解
這篇文章主要介紹了SpringBoot Session共享實現(xiàn)圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產(chǎn)消費(fèi)者模型方法解析
這篇文章主要介紹了Java多線程Queue、BlockingQueue和使用BlockingQueue實現(xiàn)生產(chǎn)消費(fèi)者模型方法解析,涉及queue,BlockingQueue等有關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以參考。2017-11-11idea無法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無效的解決
這篇文章主要介紹了idea無法打斷點(diǎn),單擊或雙擊代碼行左側(cè)區(qū)域無效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Spring Boot使用Spring的異步線程池的實現(xiàn)
這篇文章主要介紹了Spring Boot使用Spring的異步線程池的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02