All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.afu.validator.Implement.ChineseWordValidatorImpl Maven / Gradle / Ivy

There is a newer version: 2.4-RELEASE
Show newest version
package io.afu.validator.Implement;

import io.afu.validator.Annimation.ChineseIdCard;
import io.afu.validator.Annimation.ChineseWord;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChineseWordValidatorImpl implements ConstraintValidator {

    private Boolean allowEmpty;


    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (null == s || s.equals("")){
            return this.allowEmpty;
        }else {
            String regex = "[\\u4e00-\\u9fa5]";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(s);
            return m.find();
        }
    }

    @Override
    public void initialize(ChineseWord constraintAnnotation) {
        this.allowEmpty = constraintAnnotation.allowEmpty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy