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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。