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

com.github.commons.utils.validation.phone.PhoneValidationImpl Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package com.github.commons.utils.validation.phone;

import com.github.commons.utils.validation.ValidationUtil;
import tk.mybatis.mapper.util.StringUtil;

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

public class PhoneValidationImpl implements ConstraintValidator {

    private boolean required = false;

    @Override
    public void initialize(PhoneValidation phoneValidation) {
        required = phoneValidation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidationUtil.isPhone(value);
        }else{
            if(StringUtil.isEmpty(value)){
                return true;
            }else{
                return ValidationUtil.isPhone(value);
            }
        }
    }

    private boolean isPhone(String str,String regulation) {
        Pattern pattern = Pattern.compile(regulation);
        return pattern.matcher(str).matches();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy