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;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419037.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

31 | 深度和广度优先搜索:如何找出社交网络中的三度好友关系?

问题导入 给你一个用户&#xff0c;如何找出这个用户的所有三度&#xff08;其中包含一度、二度和三度&#xff09;好友关系&#xff1f; 搜索算法 算法是作用于具体数据结构之上的&#xff0c;深度优先搜索算法和广度优先搜索算法都是基于“图”这种数据结构的。这是因为&a…

可能是最好理解的二叉树的层序遍历

题目描述&#xff1a;二叉树的层序遍历&#xff0c;按层数输出每一层的结果数组 代码实现 class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res new ArrayList<List<Integer>>();Queue<…

Spring学习9-MyEclipse中Spring工程使用@Resource注释的问题

在MyEclipse 的Spring工程中&#xff0c;有时候要使用Resource注释来驱动Spring配置。但是在MyEclipse添加Spring开发能力的操作中&#xff0c;并没有 把相关的库添加到工程的classpath中&#xff0c;所以使用该注解时会产生找不到类的错误&#xff0c;这是由于MyEclipse带的Sp…

java 8的一些新用法

http://www.manongjc.com/article/44995.html 1.排序 按升序排 &#xff1a;Collections.sort(shopInfoVO1List, Comparator.comparing(ShopInfoVO1::getDistance)); 按降序排: Collections.sort(shopInfoVO1List, Comparator.comparing(ShopInfoVO1::getDistance).reversed()…

jdk1.8对synchronized锁的优化

synchronized 锁的优化&#xff1a;锁的四种状态-无锁&#xff0c;偏向锁、轻量级锁&#xff0c;重量级锁 1、偏向锁&#xff1a;原因是大多数时候是不存在锁竞争的&#xff0c;常常是一个线程多次获得同一个锁&#xff0c;因此如果每次都要竞争锁会增大很多没有必要付出的代价…

03.校准时间

复制代码保存为vbs,js等文件的时候,报莫名其妙的错误,把文件的编码格式保存为ANSI , utf8不行;VBS校准系统时间在有线网络行&#xff0c;无线就不行&#xff0c;请修改&#xff01;&#xff01;已解决&#xff01; - VBS求助&讨论 - 批处理之家 批处理_BAT_CMD_DOS_VBS_Per…

淘宝开发平台 java 调用实例

Java调用示例代码 更新日期&#xff1a;2016-02-06访问次数&#xff1a;53432 主要步骤 填充公共参数 填充业务参数 计算请求签名 发起API调用 获取API结果 示例代码 import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.…

LeetCode 293. Flip Game

原题链接在这里&#xff1a;https://leetcode.com/problems/flip-game/description/ 题目&#xff1a; You are playing the following Flip Game with your friend: Given a string that contains only these two characters: and -, you and your friend take turns to flip…

Redis缓存,你真的懂了吗

为什么要用缓存&#xff08;缓存的优点、场景&#xff09; &#xff08;1&#xff09;在项目中缓存是如何使用的&#xff1f; 结合你自己项目的业务来&#xff0c;你如果用了那恭喜你&#xff0c;你如果没用那不好意思&#xff0c;你硬加也得加一个场景吧&#xff01; &…

Java sdk 调用淘宝开发平台

public static void main(String[] args) throws Exception { // TOP服务地址&#xff0c;正式环境需要设置为http://gw.api.taobao.com/router/rest String serverUrl “http://gw.api.tbsandbox.com/router/rest”; String appKey “test”; // 可替换为您的沙箱环境应用的…

编写一个函数func(),将此函数的输入参数(int型)逆序输出显示,如54321 – 12345,要求使用递归,并且函数体代码不超过8行...

public class Test{  //中间变量private String res "0";  //方法public int func(int i){if(i>0){int temp i%10;res resString.valueOf(temp);func(i/10);}return Integer.valueOf(res);}public static void main(String[] args){Test tnew Test();int a…

你会用Java实现两个大数相加吗

两个大数相加(Java)* 1、是整数&#xff1b;* 2、两个数无限大&#xff0c;long都装不下&#xff1b;* 3、不能用BigInteger&#xff1b;* 4、不能用任何包装类提供的运算方法&#xff1b;* 5、两个数都是以字符串的方式提供。 * 思路&#xff1a;* 字符串逐位相加&#xff0c;…

获取淘宝开发平台的sessionKey

淘宝API调用 申请 获取session key 在调用淘宝的API时&#xff0c;我们都会用到appkey,appsecret,appsession。 1、我们申请应用就会有appkey和appsecret了 2、正式环境下获取SessionKey 注意&#xff1a;web插件平台应用和web其它应用在正式环境下是同样的获取方法 1&…

PERL 实现微信登录

get 请求: https://login.weixin.qq.com/jslogin? appidwx782c26e4c19acffb &redirect_urihttps%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage &funnew &langzh_CN &_1455501911998参数: _ 1455501911998 appid wx782…