javaweb中常用的验证工具类

来源:互联网 发布:音乐可视化软件 编辑:程序博客网 时间:2024/04/30 11:58

这是常用的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 189String 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
原创粉丝点击