• 自定义数据校验(4)---demo3---bai


    工具类:CharUtil
    
    package com.etc.util;
    
    import java.util.regex.Pattern;
    
    public class CharUtil {
     
        public static void main(String[] args) {
            String[] strArr = new String[] { "www.micmiu.com", "!@#$%^&*()_+{}[]|"'?/:;<>,.", "!¥……()——:;“”‘’《》,。?、", "不要啊", "やめて", "韩佳人", "???" };
            for (String str : strArr) {
                System.out.println("===========> 测试字符串:" + str);
                System.out.println("正则判断结果:" + isChineseByREG(str) + " -- " + isChineseByName(str));
                System.out.println("Unicode判断结果 :" + isChinese(str));
                System.out.println("详细判断列表:");
                char[] ch = str.toCharArray();
                for (int i = 0; i < ch.length; i++) {
                    char c = ch[i];
                    System.out.println(c + " --> " + (isChinese(c) ? "是" : "否"));
                }
            }
        }
     
        // 根据Unicode编码完美的判断中文汉字和符号
        private static boolean isChinese(char c) {
            Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
            if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                    || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                    || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
                return true;
            }
            return false;
        }
     
        // 完整的判断中文汉字和符号
        public static boolean isChinese(String strName) {
            char[] ch = strName.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                char c = ch[i];
                if (isChinese(c)) {
                    return true;
                }
            }
            return false;
        }
     
        // 只能判断部分CJK字符(CJK统一汉字)
        public static boolean isChineseByREG(String str) {
            if (str == null) {
                return false;
            }
            Pattern pattern = Pattern.compile("[\u4E00-\u9FBF]+");
            return pattern.matcher(str.trim()).find();
        }
     
        // 只能判断部分CJK字符(CJK统一汉字)
        public static boolean isChineseByName(String str) {
            if (str == null) {
                return false;
            }
            // 大小写不同:\p 表示包含,\P 表示不包含
            // \p{Cn} 的意思为 Unicode 中未被定义字符的编码,\P{Cn} 就表示 Unicode中已经被定义字符的编码
            String reg = "\p{InCJK Unified Ideographs}&&\P{Cn}";
            Pattern pattern = Pattern.compile(reg);
            return pattern.matcher(str.trim()).find();
        }
    }
    

      

    MyUsernameValidator
    
    
    package com.etc.validator;
    
    import com.etc.util.CharUtil;
    import com.opensymphony.xwork2.validator.ValidationException;
    import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
    
    public class MyUsernameValidator extends FieldValidatorSupport 
    {
    	
    	//执行校验
    	public void validate(Object ctx) throws ValidationException {
    		//1  获取输入的字段名
    		String  field  = this.getFieldName();
    		//2 获取该字段的值
    		String value = (String) this.getFieldValue(field,ctx);
    		//3 如果含有中文,则报错
    		if(CharUtil.isChinese(value))
    		{
    			this.addFieldError(field, ctx);
    		}		
    	}
    
    }
    

      

  • 相关阅读:
    教你作一份高水准的简历
    python并发
    阻塞,非阻塞,同步,异步
    python三层架构
    paramiko与ssh
    python-进程
    生产者消费者模型
    python-线程
    python-socket
    python-mysql
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6261585.html
Copyright © 2020-2023  润新知