Commit 81faa4b6 by zhiwj

自定义字段验证

parent 5138d4f1
......@@ -5,10 +5,10 @@ package com.gic.store.constant;
* @date 2019/7/1 9:20 AM
*/
public enum StoreFieldTextEnum {
Chinese(1, "中文", "[\\u4e00-\\u9fa5]{0,}"),
Number(2, "数字", "[0-9]*"),
ABC(3, "字母", "[A-Za-z]+"),
Mark(4, "符号", "[^\\w\\s]+");
Chinese(1, "中文", ".*?[\\u4e00-\\u9fa5]+.*?"),
Number(2, "数字", ".*?[0-9]+.*?"),
ABC(3, "字母", ".*?[A-Za-z]+.*?"),
Mark(4, "符号", ".*?[^0-9A-Za-z\\u4e00-\\u9fa5]+.*?");
private int code;
private String message;
......
......@@ -6,7 +6,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @author zhiwj
......@@ -71,7 +70,7 @@ public class TextField extends FieldBase {
textTypeList.remove(Integer.valueOf(s));
}
for (StoreFieldTextEnum storeFieldTextEnum : textTypeList.values()) {
if (Pattern.matches(storeFieldTextEnum.getPattern(), value)) {
if (value.matches(storeFieldTextEnum.getPattern())) {
return "不能出现" + storeFieldTextEnum.getMessage() + "类型";
}
}
......@@ -83,4 +82,40 @@ public class TextField extends FieldBase {
}
return null;
}
public static void main(String[] args) {
TextField textField = new TextField();
textField.setIsMust(1);
textField.setMaxLength(30);
textField.setMinLength(1);
textField.setTextType("2,4");
//中文
//数字
//字母
//符号
String 中文 = "发动机看了";
String 数字 = "1234";
String 字母 = "klsdf";
String 符号 = "&^*";
String 中文数字 = 中文 + 数字;
String 中文字母 = 中文 + 字母;
String 中文符号 = 中文 + 符号;
String 数字字母 = 数字 + 字母;
String 数字符号 = 数字 + 符号;
String 字母符号 = 字母 + 符号;
System.out.println("中文:" + textField.validate(中文));
System.out.println("数字:" + textField.validate(数字));
System.out.println("字母:" + textField.validate(字母));
System.out.println("符号:" + textField.validate(符号));
System.out.println("中文数字:" + textField.validate(中文数字));
System.out.println("中文字母:" + textField.validate(中文字母));
System.out.println("中文符号:" + textField.validate(中文符号));
System.out.println("数字字母:" + textField.validate(数字字母));
System.out.println("数字符号:" + textField.validate(数字符号));
System.out.println("字母符号:" + textField.validate(字母符号));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment