国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java正則表達式表單驗證類工具類(驗證郵箱、手機號碼、qq號碼等)

2019-11-26 15:31:38
字體:
來源:轉載
供稿:網友

java使用正則表達式進行表單驗證工具類,可以驗證郵箱、手機號碼、qq號碼等

復制代碼 代碼如下:

package util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 使用正則表達式進行表單驗證
 *
 */

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);
    }

    /**
     * 驗證手機號碼
     *
     * 移動號碼段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147
     * 聯通號碼段: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);
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大理市| 芒康县| 南和县| 罗甸县| 新巴尔虎右旗| 五莲县| 灵武市| 西贡区| 思茅市| 开原市| 普兰店市| 临泽县| 华容县| 曲麻莱县| 定安县| 和政县| 宁河县| 出国| 铜川市| 大邑县| 张家界市| 兴义市| 淮南市| 延边| 建始县| 天台县| 芒康县| 阿克陶县| 咸宁市| 通江县| 军事| 木兰县| 韶山市| 开阳县| 若羌县| 梓潼县| 永昌县| 平山县| 深水埗区| 双辽市| 胶南市|