javaweb中常用的验证工具类

这是常用的java工具类


public class Validate {

	/**
	 * 判断是否为整数
	 * 
	 * @param num
	 *            字符串
	 * @return 字符串为数字字符串返回true ,否则返回false
	 */
	public static boolean isInt(String num) {

		boolean flag = false;
		if (num == null) {
			return flag;
		}
		String regex = "[-]?\\d";
		regex += "+";

		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(num);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 验证是否为整数
	 * 
	 * @param num
	 *            字符串
	 * @param minLen
	 *            最小长度
	 * @param maxLen
	 *            最大长度
	 * @return 是整数返回true
	 */
	public static boolean isInt(String num, int minLen, int maxLen) {
		boolean flag = false;
		if (num == null || minLen > maxLen || minLen < 0) {
			return flag;
		}
		String regex = "\\d";
		if (minLen == 0 && maxLen == 0) {
			regex += "+";
		} else {
			regex += "{" + minLen + "," + maxLen + "}";
		}
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(num);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 判断是否为小数
	 * 
	 * @param num
	 *            字符串
	 * @return 字符串为小数字符串返回true ,否则返回false
	 */
	public static boolean isFloat(String num, int dd) {
		boolean flag = false;
		String regex = "[-]?\\d+";
		if (dd > 0) {
			regex += "\\.?\\d{0," + dd + "}";
		} else if (dd <= 0) {
			regex += "\\.?\\d+";
		}
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(num);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 验证是否为金额
	 * 
	 * @param amount
	 *            字符串
	 * @param maxAmount
	 *            最大金额
	 * @return 是金额返回true
	 */
	public static boolean isAmount(String amount, float maxAmount) {
		boolean flag = false;
		if (Validate.isFloat(amount, 2)) {
			if (Float.parseFloat(amount) > 0 && Float.parseFloat(amount) < maxAmount) {
				flag = true;
			}
		}
		return flag;
	}

	/**
	 * 验证是否为IP
	 * 
	 * @param ip
	 *            字符串
	 * @return 是IP返回true
	 */
	public static boolean isIP(String ip) {

		boolean flag = false;
		if (ip != null) {
			Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
			Matcher m = p.matcher(ip);
			if (m.matches()) {
				flag = true;
			}
		}
		if (flag) {
			String[] temp = ip.split("\\.");
			for (int i = 0; i < temp.length; i++) {
				if (Integer.parseInt(temp[i]) > 255) {
					flag = false;
					break;
				}
			}
		}
		return flag;
	}

	/**
	 * 是否为邮箱
	 * 
	 * @param email
	 *            字符串
	 * @return 是邮箱返回true
	 */
	public static boolean isEMail(String email) {
		boolean flag = false;
		if (email == null) {
			return false;
		}
		Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");// "\\w+\\.?\\w+@\\w+\\.?\\w*\\.?\\w*");
		Matcher m = p.matcher(email);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 验证是否是手机号格式 该方法还不是很严谨,只是可以简单验证
	 * 
	 * @param mobile
	 * @return true表示是正确的手机号格式,false表示不是正确的手机号格式
	 */
	public static boolean isMobile(String mobile) {
		// 当前运营商号段分配
		// 中国移动号段 1340-1348 135 136 137 138 139 150 151 152 157 158 159 187 188
		// 147
		// 中国联通号段 130 131 132 155 156 185 186 145
		// 中国电信号段 133 1349 153 180 189
		String regular = "1[3,4,5,8]{1}\\d{9}";
		Pattern pattern = Pattern.compile(regular);
		boolean flag = false;
		if (mobile != null) {
			Matcher matcher = pattern.matcher(mobile);
			flag = matcher.matches();
		}
		return flag;
	}

	/**
	 * 验证身份证格式
	 * 
	 * @param mobile
	 * @return true表示是正确的身份证格式,false表示不是正确的身份证格式
	 */
	public static boolean isIdCard(String idCard) throws ParseException {
		return ValidateIdentityCard.isCardId(idCard);
	}

	public static void main(String[] args) {
		String tel = "13811028050";
		boolean flag = Validate.isMobile(tel);
		System.out.println(flag);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值