zoukankan      html  css  js  c++  java
  • 正则表达式校验数据

    package com.midea.jr.efc.eac.commons;
    
    import java.util.regex.Pattern;
    
    import com.midea.jr.commons.utils.AssertUtil;
    
    
    public class Validator {
        
        private static final String NUMWORD = "^[^u4e00-u9fa5]{0,}$"; //非汉字
        private static final String NUMWORDMINUS = "^[\-]*[A-Za-z0-9]+$"; //数字和字母
        private static final String CHINESE = "^^[u4E00-u9FA5uF900-uFA2D]+$"; //中文
        private static final String NUMBER = "^[0-9]+$"; //数字
        private static final String WORD = "^[u4e00-u9fa5_a-zA-Z0-9]+$"; //中文,字母,数字
        
        public static boolean check(String reg,String value){
            return Pattern.matches(reg,value); 
        }
        
        public static boolean checkNumWord(String v){
            return check(NUMWORD,v); 
        }
        
        public static boolean checkNumWordMinus(String v){
            return check(NUMWORDMINUS,v); 
        }
        /**
         * 是否数字
         * @param v
         * @return
         */
        public static boolean checkNumber(String v){
            return check(NUMBER,v); 
        }
        /**
         * 是否由中文,字母,数字组成
         * @param v
         * @return
         */
        public static boolean checkWord(String v){
            return check(WORD,v); 
        }
        
        public static boolean checkChinese(String v){
            return check(CHINESE,v); 
        }
    
        public static boolean isNull(String v){
            if(v == null||v.equals("")){
                return true;
            }
            return false; 
        }
    
        public static void main(String[] args) {
    //        System.out.println(checkChinese("哈哈a3哈"));
    //        System.out.println(checkNumWord("33所属aa"));
    //        System.out.println(Pattern.matches(NUMBER,"23"));
    //        System.out.println(Pattern.matches(WORD,"23a号"));
    //        AssertUtil.isTrue(Validator.checkNumber("a"),ErrorCodeEnum.TYPE_VALID.getErrordesc());
    //        AssertUtil.isTrue(Validator.checkWord("a"),ErrorCodeEnum.TYPE_VALID.getErrordesc());
            
    //        System.out.println(Validator.checkNumWord("aaa_"));
            System.out.println(Validator.check("(^([u4E00-u9FA5uF900-uFA2D]){1,20}$)||(^([A-Za-z]){2,40}$)","哈哈成擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦擦哈"));
            
        }
    }
  • 相关阅读:
    web页面常用方法及INI文件的读取方法
    winform 三个Panel左右切换(panel里面填充图片)
    图片渐出轮播的效果
    Winform跑马灯——Graphics运用
    .net 3.5 新功能重写ToInt()方法
    style.display
    SQL: 分页SQL SQL2005函数分页!
    JS: 验证输入必须为数字
    Table 里面点标题会进行排序
    在Div中绑定数据
  • 原文地址:https://www.cnblogs.com/yun965861480/p/6548055.html
Copyright © 2011-2022 走看看