• Java代码使用正则验证和常用工具方法


    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-u9fa5uFF21-uFF3AuFF41-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;
    	}
    
  • 相关阅读:
    开天辟地第一人---盘古
    质量管理三个概念:QC、QA和QM,你能分得清吗?
    项目管理PV、EV、AC、BAC、EAC、ETC等计算
    信息系统项目管理师考试是高级职称资格考试,可以用来评高级职称
    项目章程
    信息系统项目管理师---项目管理5大过程组十大知识域47个过程
    一句话信息系统项目管理
    信息系统项目管理师论文写作经验
    Linux系统
    什么是Redis?
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5492689.html
Copyright © 2020-2023  润新知