校验数据有效性工具类
来源:互联网 发布:陈炳勇力量数据 编辑:程序博客网 时间:2024/05/22 12:02
功能
- 验证用户姓名且字符长度为1~50即2~25个汉字
- 邮箱验证
- 验证手机格式
- 判断输入是否为数字
- 判断手机号是否合法
- 验证联系方式
- 验证金额有效性
- 判断是否含中文
- 判断是否为纯英文
- 判断是否为纯中文
- 判断是否为密码类型
- 判断是否为警员号
- 判断邮件email格式是否正确
- 用于判断用户输入字符长度6到20位
- 用于判断用户输入住址字符长度1到250位
- 用于判断用户输入描述字符长度1到50位
- 用于判断用户真名长度
- 用于判断用户输入字符首字母是否英文
- 用于判断银行卡位数13到19位
- 中文占两字符 英文占一个
- 过滤特殊字符
- 验证输入密码长度(6-20位除空格回车tab外字符)
- 若str符合regex的正则表达式格式返回true,否则返回false
- 判断字符串是否为日期格式
- 根据身份证号获取用户生日
- 根据身份证号获取用户性别
代码
package com.example.administrator.individualresume.utils;import android.text.TextUtils;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created on 2017/9/15 * Author 郑少鹏 * <p> * 验证数据有效性 */public class CheckOutUtil { /** * 验证用户姓名且字符长度为1~50即2~25个汉字 * * @param userName * @return */ public static boolean isUserName(String userName) { Pattern pattern = Pattern.compile("^([\u4E00-\u9FA5]|[\uF900-\uFA2D]|[\u258C]|[\u2022]|[\u2E80-\uFE4F])+$"); Matcher mc = pattern.matcher(userName); return mc.matches(); } /** * 邮箱验证 * * @param mail * @return */ public static boolean isValidEmail(String mail) { Pattern pattern = Pattern.compile("^[A-Za-z0-9][\\w\\._]*[a-zA-Z0-9]+@[A-Za-z0-9-_]+\\.([A-Za-z]{2,4})"); Matcher mc = pattern.matcher(mail); return mc.matches(); } /** * 验证手机格式 */ public static boolean isMobileNO(String mobiles) { /* * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 * 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通)147 177 * 总结,即第一位必为1,第二位必为3、5、8,其他位置可为0-9 */ String telRegex = "[1][34578]\\d{9}";// "[1]"代表第1位数字为1,"[358]"代表第二位可为3、5、8中任一个,"\\d{9}"代表后面可为0~9中任一数字,共9位 if (TextUtils.isEmpty(mobiles)) return false; else return mobiles.matches(telRegex); } /** * 判断输入是否为数字 */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } /** * 判断手机号是否合法 */ public static boolean isPhone(String mobiles) { // Pattern p = Pattern.compile("^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$"); Pattern p = Pattern.compile("1[0-9]{10}"); Matcher m = p.matcher(mobiles); return m.matches(); } /** * 验证联系方式 * * @param str 座机或手机 * @return */ public static boolean isMobileOrPhone(String str) { String regex = "^((([0\\+]\\d{2,3}-)|(0\\d{2,3})-))(\\d{7,8})(-(\\d{3,}))?$|^1[0-9]{10}$"; return match(regex, str); } /** * 验证金额有效性 * * @param price * @return */ public static boolean isPrice(String price) { String regex = "^([1-9][0-9]{0,7})(\\.\\d{1,2})?$"; return match(regex, price); } /** * 判断是否含中文 */ public static boolean isContainChinese(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; } /** * 判断是否为纯英文 */ public static boolean isLetter(String str) { Pattern p = Pattern.compile("^[A-Za-z]+$"); Matcher m = p.matcher(str); return m.matches(); } /** * 判断是否为纯中文 */ public static boolean isChiness(String str) { Pattern p = Pattern.compile("^[\u4E00-\u9FFF]+$"); Matcher m = p.matcher(str); return m.matches(); } /** * 判断是否为密码类型 */ public static boolean isPassword(String str) { Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z.*]{6,20}$"); Matcher m = p.matcher(str); return m.matches(); } /** * 判断是否为警员号 */ public static boolean isPoliceNumberAndLength(String str) { Pattern p = Pattern.compile("[A-Za-z0-9]{6,12}"); Matcher m = p.matcher(str); return m.matches(); } /** * 判断邮件email格式是否正确 */ public static boolean isEmail(String email) { if (null == email || "".equals(email)) return false; // Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); // 简单匹配 Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");// 复杂匹配 Matcher m = p.matcher(email); return m.matches(); } /** * 用于判断用户输入字符长度6到20位 * * @param str * @return */ public static boolean isLength(String str) { Pattern pattern = Pattern.compile(".{6,20}"); return pattern.matcher(str).matches(); } /** * 用于判断用户输入住址字符长度1到250位 * * @param str * @return */ public static boolean isAddressLength(String str) { Pattern pattern = Pattern.compile(".{1,250}"); return pattern.matcher(str).matches(); } /** * 用于判断用户输入描述字符长度1到50位 * * @param str * @return */ public static boolean isRemarksLength(String str) { Pattern pattern = Pattern.compile(".{1,50}"); return pattern.matcher(str).matches(); } /** * 用于判断用户真名长度 * * @param str * @return */ public static boolean isNameLength(String str) { Pattern pattern = Pattern.compile(".{2,25}"); return pattern.matcher(str).matches(); } /** * 用于判断用户输入字符首字母是否英文 * * @param str * @return */ public static boolean isEnglish(String str) { Pattern pattern = Pattern.compile("[a-zA-Z][a-zA-Z0-9]+"); return pattern.matcher(str).matches(); } /** * 用于判断银行卡位数13到19位 * * @param str * @return */ public static boolean isbank(String str) { Pattern pattern = Pattern.compile(".{13,19}"); return pattern.matcher(str).matches(); } /** * 中文占两字符 * 英文占一个 */ public static int String_length(String value) { int valueLength = 0; String chinese = "[\u4e00-\u9fa5]"; for (int i = 0; i < value.length(); i++) { String temp = value.substring(i, i + 1); if (temp.matches(chinese)) { valueLength += 2; } else { valueLength += 1; } } return valueLength; } /** * 过滤特殊字符 */ public static String stringFilter(String str) { String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll(""); } /** * 验证输入密码长度(6-20位除空格回车tab外字符) */ public static boolean isPasswLength(String str) { String regex = "^\\S{6,20}$"; return match(regex, str); } /** * @param regex 正则表达式字符串 * @param str 所匹配字符串 * @return 若str符合regex的正则表达式格式返回true,否则返回false */ private static boolean match(String regex, String str) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.matches(); } /** * 判断字符串是否为日期格式 * * @return */ public static boolean isDate(String strDate) { Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"); Matcher m = pattern.matcher(strDate); if (m.matches()) { return true; } else { return false; } } /** * 根据身份证号获取用户生日 * * @return */ public static String getUserBrithdayByCardId(String ids) { String birthday = ""; if (ids.length() == 18) { birthday = ids.substring(6, 14);// 18位 String years = birthday.substring(0, 4); String moths = birthday.substring(4, 6); String days = birthday.substring(6, 8); birthday = years + "-" + moths + "-" + days; } else if (ids.length() == 15) { birthday = ids.substring(6, 12);// 15位 String years = birthday.substring(0, 2); String moths = birthday.substring(2, 4); String days = birthday.substring(4, 6); birthday = "19" + years + "-" + moths + "-" + days; } return birthday; } /** * 根据身份证号获取用户性别 * * @return */ public static String getUserSexByCardId(String ids) { String sexShow = ""; if (ids.length() == 18) { String sexString = ids.trim().substring(ids.length() - 2, ids.length() - 1);// 获取身份证倒数第二位 int sexNum = Integer.parseInt(sexString);// 转换为数字 if (sexNum % 2 != 0) { sexShow = "男"; } else { sexShow = "女"; } } else if (ids.length() == 15) { String sexString = ids.trim().substring(ids.length() - 1, ids.length());// 获取身份证最后一位 int sexNum = Integer.parseInt(sexString);// 转换为数字 if (sexNum % 2 != 0) { sexShow = "男"; } else { sexShow = "女"; } } return sexShow; }}
阅读全文
0 0
- 校验数据有效性工具类
- Android 身份证号有效性校验工具类
- Java工具类实现校验公民身份证的有效性
- Java工具类实现校验邮箱地址的有效性
- Java工具类实现校验手机号码的有效性
- Java工具类实现校验公民身份证的有效性
- Java工具类实现校验公民身份证的有效性
- UAP平台常见数据有效性数据校验
- fix协议封装挑战-数据有效性校验
- 有效性校验
- Java开发实现对身份证有效性校验及获得户籍、性别的工具类
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法!
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- 浅谈在ASP.NET中数据有效性校验的方法
- Spark中RDD转换成DataFrame的两种方式(分别用Java和scala实现)
- docker实现数据库的主从复制
- 理解前端数据双向绑定原理——Object.defineProperty()
- Jredis--java使用redis
- oracle 10g的安装6.5
- 校验数据有效性工具类
- 谷歌搜索引擎优化必须知道的三个点
- 38.常用类
- docker官方父镜像制作
- PAM
- 与HTTP有什么区别?HTTPS的七个误解
- 常用换算
- 前端常见疑难杂症(部分google浏览器)
- shell脚本初入门必读