android驗證各種判斷

jopen 10年前發布 | 35K 次閱讀 Android Android開發 移動開發

public class RegularUtil {

public static boolean checkName(Activity context, String name) {
    if (TextUtils.isEmpty(name) || name.length() < 3 || name.length() > 16 || !nameFormat(name)) {
        AppMsg.makeText(context, "昵稱不符合規范,3-16個中英文字符、數字", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkHeight(Activity context, int height) {
    if (height < 100 || height > 250) {
        AppMsg.makeText(context, "身高超出正常范圍", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkWeight(Activity context, int weight) {
    if (weight < 40 || weight > 250) {
        AppMsg.makeText(context, "體重超出正常范圍", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkStepSize(Activity context, int stepSize) {
    if (stepSize < 30 || stepSize > 150) {
        AppMsg.makeText(context, "步長超出正常范圍", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkEmail(Activity context, String email) {
    if (!emailFormat(email) || email.length() > 31) {
        AppMsg.makeText(context, "郵箱格式不正確", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkPassword(Activity context, String password) {
    if (!passwordFormat(password)) {
        AppMsg.makeText(context, "密碼格式是6-15位英文字符、數字", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean checkPassword(Activity context, String password, String confirm) {
    if (!checkPassword(context, password)) {
        return false;
    }
    if (!password.equals(confirm)) {
        AppMsg.makeText(context, "登錄密碼設置不一致");
        return false;
    }
    return true;
}

public static boolean checkCode(Activity context, String code) {
    if (code.length() != 4) {
        AppMsg.makeText(context, "請輸入正確的四位驗證碼", AppMsg.STYLE_ALERT).show();
        return false;
    }
    return true;
}

public static boolean check(Activity context, String email, String password) {
    if (!emailFormat(email) || email.length() > 31) {
        AppMsg.makeText(context, "郵箱格式不正確", AppMsg.STYLE_ALERT).show();
        return false;
    }
    if (!checkPassword(context, password)) {
        return false;
    }
    return true;
}

private static boolean emailFormat(String email) {
    Pattern pattern = Pattern.compile("^[A-Za-z\\d]+(\\.[A-Za-z\\d]+)*@([\\dA-Za-z](-[\\dA-Za-z])?)+(\\.{1,2}[A-Za-z]+)+$");
    Matcher mc = pattern.matcher(email);
    return mc.matches();
}

/**
 * 以字母開頭,長度在3~16之間,只能包含字符、數字和下劃線(w)
 * 
 * @param password
 * @return
 */
private static boolean passwordFormat(String password) {
    Pattern pattern = Pattern.compile("^[\\@A-Za-z0-9\\!\\#\\$\\%\\^\\&\\*\\.\\~]{6,15}$");
    Matcher mc = pattern.matcher(password);
    return mc.matches();
}

public static boolean nameFormat(String name) {
    Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5A-Za-z0-9_]{3,16}$");
    Matcher mc = pattern.matcher(name);
    return mc.matches();
}

/**
 * 獲取含雙字節字符的字符串字節長度
 * 
 * @param s
 * @return
 */
public static int getStringLength(String s) {
    char[] chars = s.toCharArray();
    int count = 0;
    for (char c : chars) {
        count += getSpecialCharLength(c);
    }
    return count;
}

/**
 * 獲取字符長度:漢、日、韓文字符長度為2,ASCII碼等字符長度為1
 * 
 * @param c
 *            字符
 * @return 字符長度
 */
private static int getSpecialCharLength(char c) {
    if (isLetter(c)) {
        return 1;
    } else {
        return 2;
    }
}

/**
 * 判斷一個字符是Ascill字符還是其它字符(如漢,日,韓文字符)
 * 
 * @param char c, 需要判斷的字符
 * @return boolean, 返回true,Ascill字符
 */
private static boolean isLetter(char c) {
    int k = 0x80;
    return c / k == 0 ? true : false;
}

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!