標籤:android style blog ar color sp for on div
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; }}
android 驗證一