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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值