常用的输入检测集合(不定时补充)

常用是输入检测


    输入数据是每一个应用或软件必不可少的获取信息的方式,然而用户的输入存在极大的不确定性,正因为不确定性极大所以输入检测才显得尤为的重要。输入检测是良好应用或软件的基础,输入检查可以提高软件的容错率进而提高用户的体验感!如下为一些常用的输入检测(详细内容直接看代码):

1、字符串为空检测(作为基础判断)
/**
	 * 字符串为空检测<br/>
	 * 1、str引用未初始化<br/>
	 * 2、str指向的字符串长度为0<br/>
	 * 
	 * @param str
	 * @return 是否为字符串为空
	 */
	public static boolean isEmpty(String str) {
		// 为空
		if (str == null || "".equals(str)) {
			return true;
		}

		return false;
	}

2、第二代身份证校验
/**
	 * 第二代身份证校验<br/>
	 * 18位身份证号码组成:<br/>
	 * ddddddyyyymmddxxsp共18位,年份代码由原来的2位升级到4位,其他部分都和15位的相同。<br/>
	 * 其中: 最后一位为校验位。<br/>
	 * 校验规则是: <br/>
	 * (1)十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和<br/>
	 * Ai:表示第i位置上的身份证号码数字值 <br/>
	 * Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 <br/>
	 * (2)计算模 Y = mod(S, 11) <br/>
	 * (3)通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2<br/>
	 *
	 * @param id
	 *            身份证字符串
	 * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	private static boolean isIdCard2th(String id) {
		// 为空
		if (isEmpty(id)) {
			return false;
		}
		// 前面为17位数字 后一位为X或数字
		if (!id.matches("^\\d{17}(\\d|[xX])$")) {
			return false;
		}
		// 身份证是否合法
		// 前17的系数
		int[] coe = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		int count1_17 = 0, num_18 = -1;
		// 算出前17位的和 与后一位的值
		char c;
		for (int i = 0; i < 18; i++) {
			c = id.charAt(i);
			if (i < 17) {
				count1_17 += (c - '0') * coe[i];
			} else {
				num_18 = (c == 'X' || c == 'x') ? 10 : c - '0';
			}
		}
		// 后一位对应的数字
		int[] mant_18 = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
		if (mant_18[count1_17 % 11] != num_18)
			return false;
		// 身份证正确
		return true;
	}

3、验证邮箱
/**
	 * 验证邮箱<br/>
	 * regex =
	 * "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
	 * <br/>
	 * 
	 * @param str
	 *            待验证的字符串
	 * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isEmail(String str) {
		if (isEmpty(str)) {
			return false;
		}
		String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		return str.matches(regex);
	}

4、验证IP地址
/**
	 * 验证IP地址<br/>
	 * 
	 * @param ip
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isIP(String ip) {
		if (isEmpty(ip)) {
			return false;
		}
		//数字大小在0-255之间
		String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
		String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
		return ip.matches(regex);
	}

5、验证网址Url
/**
	 * 验证网址Url<br/>
	 * regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"<br/>
	 * 
	 * @param url
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isUrl(String url) {
		if (isEmpty(url)) {
			return false;
		}
		String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
		return url.matches(regex);
	}

6、验证电话号码
/**
	 * 验证电话号码<br/>
	 * regex = "^[1-9]\\d{6}$"<br/>
	 * 
	 * @param tel
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isTelephone(String tel) {
		if (isEmpty(tel)) {
			return false;
		}
		String regex = "^((\\d{3,4}-)|\\d{3,4}-)?\\d{7,8}$";

		return tel.matches(regex);
	}

7、验证手机号码(手机号做了最简单的匹配)
/**
	 * 验证手机号码<br/>
	 * regex = "1\\d{10}$"<br/>
	 * 
	 * @param tel
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isMobile(String mob) {
		if (isEmpty(mob)) {
			return false;
		}
		String regex = "1\\d{10}$";
		return mob.matches(regex);
	}

8、密码验证
/**
	 * 密码验证<br/>
	 * regex = "^[\\w]{6,16}$"<br/>
	 * 
	 * @param pwd
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isPassword(String pwd) {
		try {
			return isPassword(pwd, 6, 16);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 密码验证<br/>
	 * regex = "^[\\w]{minLen,maxLen}$"<br/>
	 * 
	 * @param pwd
	 *            待验证的字符串
	 * @param minLen
	 *            字符串最短
	 * @param maxLen
	 *            字符串最长
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 * @throws Exception
	 *             长度限制出错
	 */
	public static boolean isPassword(String pwd, int minLen, int maxLen) throws Exception {
		if (isEmpty(pwd)) {
			return false;
		}

		if (minLen < 0) {
			throw new Exception("minLen: " + minLen + " < 0" + "  最小长度不小于0");
		}
		if (minLen > maxLen) {
			throw new Exception("minLen: " + minLen + " > maxLen:" + maxLen + "  最大长度不能小于最小长度");
		}
		String regex = "^[\\w]{" + minLen + "," + maxLen + "}$";
		return pwd.matches(regex);
	}

9、验证邮政编号
/**
	 * 验证邮政编号<br/>
	 * regex = "^\\d{6}$"<br/>
	 * 
	 * @param post
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isPostalcode(String post) {
		String regex = "^\\d{6}$";
		return post.matches(regex);
	}

10、验证汉字 \u4e00为一,\u9fa5为 龠,仅为一二级字库十六进制范围
/**
	 * 验证汉字<br/>
	 * regex = "^[\u4e00-\u9fa5]*$"<br/>
	 * 
	 * @param word
	 *            待验证的字符串
	 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
	 */
	public static boolean isOnlyChinese(String word) {
		if (isEmpty(word)) {
			return false;
		}
		String regex = "^[\u4e00-\u9fa5]*$";

		return word.matches(regex);
	}

用到什么再添加补充、欢迎斧正。。。。。。








这里下载源代码














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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值