Java代码使用正则验证和常用工具方法
2024-08-28 02:02:16
1.正则验证邮箱
public static boolean checkEmail(String email){
boolean flag = false;
try{
String check = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
}catch(Exception e){
flag = false;
}
return flag;
}
2.验证昵称是否合法
/**
* 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数
* @return
*/
public static boolean validateUserName(String userName) { String validateStr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";
boolean rs = false;
rs = matcher(validateStr, userName);
if (rs) {
int strLenth = getStrLength(userName);
if (strLenth < 4 || strLenth > 15) {
rs = false;
}
}
return rs;
} /**
* 获取字符串的长度,对双字符(包括汉字)按两位计数
* @return
*/
public static int getStrLength(String value) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
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;
} private static boolean matcher(String reg, String string) {
boolean tem = false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(string);
tem = matcher.matches();
return tem;
}
3.判断是否是数字
public static boolean isInteger(String value) {
Pattern p = Pattern.compile("^[0-9]{11}$");
Matcher m = p.matcher(value);
boolean isOK = m.find();
return isOK;
}
4.创建随机数
public static int buildRandom(int length) {
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
}
5.获取机型,浏览器类型
public static String getTypeas(HttpServletRequest request) {
String typeas= request.getHeader("User-Agent");
if (typeas.equals("")||typeas==null) {
typeas="nothing";
}
return typeas;
}
5.获取IP地址
public static String getIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip="unknown";
}
return ip;
}
6.判断数字是否在数组内
public static boolean isIn(Integer substring, Integer[] source) {
if (source == null || source.length == 0) {
return false;
}
for (int i = 0; i < source.length; i++) {
Integer aSource = source[i];
if (aSource.equals(substring)) {
return true;
}
}
return false;
}
最新文章
- IOS OC 多任务定时器 NSRunLoop 管理 NSTimer
- Jquery判断数组中是否包含某个元素$.inArray()的用法
- JS实现剪切板添加网站版权、来源
- CentOS 5/6.X 使用 EPEL YUM源
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
- YUM软件管理
- Java之循环练习2
- 记录一次MVC3升级MVC4遇到的问题
- common-httpclient 用户名密码认证示例
- Moodle的qq登录版块的使用
- 【netlimiter】的使用
- vue中使用provide和inject刷新当前路由(页面)
- mysql提取.sql备份文件中的单个表以及表数据
- MySQL表与表之间的关系
- Python3虚拟环境--venv
- Mac 常用软件下载及使用教程地址推荐
- 泛型List、HashTable
- gridview空间使用
- Java基础——GUI编程(二)
- android编译错误“OnClickListener cannot be resolved to a type”