校驗各種表達式

/*
 *判斷有效用戶名正則表達式
 */
public static boolean validateUserName(String userName) {
    String validateStr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";
    boolean rs = false;
    rs = matcher(validateStr, userName);
    if (rs) {
        int strLenth = getStrLength(userName);
        if (strLenth < 2 || strLenth > 10) {
            rs = false;
        }
    }

    return rs;
}
public static int getStrLength(String value) {
    int valueLength = 0;
    String chinese = "[\u0391-\uFFE5]";
    for (int i = 0; i < value.length(); i++) {
        String temp = value.substring(i, i + 1);
        if (temp.matches(chinese)) {
            valueLength += 2;
        } else {
            valueLength += 1;
        }
    }
    return valueLength;
}

private static boolean matcher(String reg, String string) {
    boolean tem = false;
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(string);
    tem = matcher.matches();
    return tem;
}

/*
 *判斷有效手機號正則表達式
 */

public static boolean isPhoneNumber(String strPhone) {

    String str = "^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9])|(14[0-9]))\\d{8}$";
    Pattern p = Pattern.compile(str);
    Matcher m = p.matcher(strPhone);
    return m.find();
}

/*
 *判斷有效郵箱正則表達式
 */

public static boolean checkEmail(String email) {
    boolean flag = false;
    try {
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        flag = matcher.matches();
    } catch (Exception e) {
        flag = false;
    }
    return flag;
}

/*
 *判斷有效密碼正則表達式
 */
public static boolean rexCheckPassword(String input) {
    // 6-10 位,字母、數字、字符
    String regStr = "^([A-Z]|[a-z]|[0-9]|[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]){6,10}$";
    return input.matches(regStr);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章