java常见数据校验
生活随笔
收集整理的這篇文章主要介紹了
java常见数据校验
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 手機號驗證* * @param str* @return 驗證通過返回true*/public static boolean isMobile(String str) {Pattern p = null;Matcher m = null;boolean b = false;p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 驗證手機號m = p.matcher(str);b = m.matches();return b;}/*** 電話號碼驗證* * @param str* @return 驗證通過返回true*/public static boolean isPhone(String str) {Pattern p1 = null, p2 = null;Matcher m = null;boolean b = false;p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 驗證帶區號的p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 驗證沒有區號的if (str.length() > 9) {m = p1.matcher(str);b = m.matches();} else {m = p2.matcher(str);b = m.matches();}return b;}/*** 驗證郵箱* * @param email* @return*/public static boolean checkEmail(String email) {boolean flag = false;try {String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";Pattern regex = Pattern.compile(check);Matcher matcher = regex.matcher(email);flag = matcher.matches();} catch (Exception e) {flag = false;}return flag;}/*** 短信驗證碼(6位數字)*/public static boolean checkMsgCode(String code) {boolean flag = false;try {String check = "^[0-9]{6}";Pattern regex = Pattern.compile(check);Matcher matcher = regex.matcher(code);flag = matcher.matches();} catch (Exception e) {flag = false;}return flag;}/*** 驗證日期格式* @param date yyyy-mm-dd* @return*/public static boolean checkDate(String date) {if (date == null) return false;try {String check = "[0-9]{4}-[0-9]{2}-[0-9]{2}";Pattern regex = Pattern.compile(check);Matcher matcher = regex.matcher(date);if (!matcher.matches()) return false;String data[] = date.split("-");int year = Integer.parseInt(data[0]);int month = Integer.parseInt(data[1]);int day = Integer.parseInt(data[2]);boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)|| (year % 400 == 0);if (!(month >= 1 && month <= 12)) return false;if (!(day >= 1 && day <= 31)) return false;if (month == 2) {int maxDay = isLeapYear ? 29 : 28;if (!(day >= 1 && day <= maxDay)) return false;}if (day == 31) {boolean isBigMonth = month == 1 ||month == 3 ||month == 5 ||month == 7 ||month == 8 ||month == 10 ||month == 12;return isBigMonth;}return true;} catch (Exception e) {return false;}} /*** 是否是Url地址* @param str* @return*/public static boolean isURL(String str){boolean flag = false;try {//轉換為小寫str = str.toLowerCase();String check = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 + "|" // 允許IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二級域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 端口- :80 + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; Pattern regex = Pattern.compile(check);Matcher matcher = regex.matcher(str);flag = matcher.matches();} catch (Exception e) {flag = false;}return flag;}/*** 是否是漢字* @param str* @return*/public static boolean isChineseCharacter(String str) {String check = "[\\u4e00-\\u9fa5]+";Pattern regex = Pattern.compile(check);Matcher matcher = regex.matcher(str);return matcher.matches();}/*** 是否是銀行卡號 * Luhn算法來驗證:* 1、從卡號最后一位數字開始,逆向將奇數位(1、3、5等等)相加。* 2、從卡號最后一位數字開始,逆向將偶數位數字,先乘以2(如果乘積為兩位數,則將其減去9),再求和。* 3、將奇數位總和加上偶數位總和,結果應該可以被10整除。* @param number* @return*/public static boolean isBankerNumber(String number) {if (number == null ) return false;if (number.length() != 16 && number.length() != 19) return false;if (!number.matches("\\d+")) return false;char digits[] = number.toCharArray();int len = number.length();int numSum = 0;for(int i = len - 1,j = 1; i >= 0; i--,j++) {int value = digits[i] - '0';if (j % 2 == 0) {value *= 2;if (value > 9) value -= 9;}numSum += value;}return numSum % 10 == 0;}
總結
以上是生活随笔為你收集整理的java常见数据校验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一些密码学数学基础
- 下一篇: influxdb mysql对比_Inf