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

最新文章

  1. IOS OC 多任务定时器 NSRunLoop 管理 NSTimer
  2. Jquery判断数组中是否包含某个元素$.inArray()的用法
  3. JS实现剪切板添加网站版权、来源
  4. CentOS 5/6.X 使用 EPEL YUM源
  5. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
  6. YUM软件管理
  7. Java之循环练习2
  8. 记录一次MVC3升级MVC4遇到的问题
  9. common-httpclient 用户名密码认证示例
  10. Moodle的qq登录版块的使用
  11. 【netlimiter】的使用
  12. vue中使用provide和inject刷新当前路由(页面)
  13. mysql提取.sql备份文件中的单个表以及表数据
  14. MySQL表与表之间的关系
  15. Python3虚拟环境--venv
  16. Mac 常用软件下载及使用教程地址推荐
  17. 泛型List、HashTable
  18. gridview空间使用
  19. Java基础——GUI编程(二)
  20. android编译错误“OnClickListener cannot be resolved to a type”

热门文章

  1. 建议37:按需选择sort或sorted
  2. js实现给一个数组监听
  3. box-flex兼容写法
  4. ACM训练小结-2018年6月16日
  5. INSPIRED启示录 读书笔记 - 前言
  6. c++ boost库学习三:实用工具
  7. ios开发在导入环信SDK后运行出现 Reason: image not found 的解决方案
  8. hql join
  9. spring mvc 对象型参数的传递(遇到坑了)
  10. HBase-过滤器(各种过滤器及代码实现)