这是一个比较简单的表单校验,利用责任链模式将多个数据校验以“链”的形式串联起来。
目前支持三种数据校验(可拓展)
1.空数据校验
2.数据格式校验
3.非必填项校验
代码:定义校验器
public interface Verify {
//校验
boolean doVerify();
//设置下个校验器
void setNextVerify(Verify verify);
//显示错误信息
boolean showErrorMsg(String errorMsg);
//执行校验回调(提供支持,项目未使用到)
interface VerifyCallBack {
//成功
void sussecc();
//失败
void failure();
}
}
抽象校验器
public abstract class AbsVerify implements Verify {
protected String data;//校验数据
protected String errorMsg;//失败信息
protected Verify nextVerify; //下个执行的校验器
protected VerifyCallBack callBack;
public AbsVerify(String data, String errorMsg) {
this.data = data;
this.errorMsg = errorMsg;
}
public void setNextVerify(Verify verify) {
this.nextVerify = verify;
}
@Override
public boolean showErrorMsg(String errorMsg) {
if (callBack != null) {
callBack.failure();
}
if (!isEmpty(errorMsg)) {
System.out.println(errorMsg);
}
return false;
}
//执行下一个校验
protected boolean doNextFilter() {
if (callBack != null) {
callBack.sussecc();
}
return nextVerify != null ? nextVerify.doVerify() : true;
}
//是否为空
protected boolean isEmpty(String msg) {
return StringUtil.isBlank(msg);
}
public VerifyCallBack getCallBack() {
return callBack;
}
public void setCallBack(VerifyCallBack callBack) {
this.callBack = callBack;
}
}
空数据校验
public class EmptyVerify extends AbsVerify {
public EmptyVerify(String data, String errorMsg) {
super(data, errorMsg);
}
@Override
public boolean doVerify() {
return isEmpty(data) ? showErrorMsg(errorMsg) : doNextFilter();
}
}
数据格式校验(使用正则表达式)
public class RegularVerify extends AbsVerify {
protected String regular;//正则表达式
public RegularVerify(String data, String regular, String errorMsg) {
super(data,errorMsg);
this.regular = regular;
}
//是否允许为空
protected boolean allowEmpty() {
return false;
}
@Override
public boolean doVerify() {
if (isEmpty(data)) {
return allowEmpty() ? doNextFilter() : showErrorMsg(errorMsg);
}
return matcher() ? doNextFilter() : showErrorMsg(errorMsg);
}
//正则表达式
private boolean matcher() {
Pattern p = Pattern.compile(regular);
Matcher m = p.matcher(data);
return m.matches();
}
}
非必填项校验
public class AllowEmptyVerify extends RegularVerify {
public AllowEmptyVerify(String data, String regular, String errorMsg) {
super(data, regular, errorMsg);
}
@Override
protected boolean allowEmpty() {
return true;
}
}
校验管理
public class VerifyManger {
//校验器集合
private List verifies = new ArrayList<>();
//添加校验器
public VerifyManger addVerify (Verify verify) {
verifies.add(verify);
return this;
}
//执行校验器
public boolean execute() {
if (verifies.isEmpty()) {
return true;
}
int size = verifies.size();
if (size == 1) {
return verifies.get(0).doVerify();
}
for (int i = 0; i < size; i++) {
Verify verify = verifies.get(i);
if (i + 1 < size) {
verify.setNextVerify(verifies.get(i + 1));
}
}
return verifies.get(0).doVerify();
}
}
正则表达式
public class Regular {
//密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)
public static final String PASSWORD = "^[a-zA-Z]\\w{6,18}$";
//用户名(中文、英文、数字包括下划线)
public static final String NAME = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$";
//手机号
public static final String PHONE = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
}
使用案例场景
登录界面用户需要填写用户名(必填)、密码(必填)、手机号(非必填),点击登录进行校验
public class Test {
static String name = "童心未泯佐为";
static String password = "qqwwee123";
static String phone = "";
public static void main(String args[]) {
boolean execute = new VerifyManger()
.addVerify(new EmptyVerify(name, "请填写用户名"))
.addVerify(new RegularVerify(name, Regular.NAME, "用户名格式不正确"))
.addVerify(new EmptyVerify(password, "请填写密码"))
.addVerify(new RegularVerify(password, Regular.PASSWORD, "密码格式不正确"))
.addVerify(new AllowEmptyVerify(phone, Regular.PHONE, "手机号格式不正确"))//非必填 允许为空 有值校验
.execute();
if (execute) {
System.out.println("校验通过");
}
}
}
结果
校验通过