han-note项目初始化完毕!
- 自定义邮箱校验注解
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.hanserwei.framework.common.validate;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = EmailValidator.class)
|
||||
public @interface EmailNumber {
|
||||
|
||||
String message() default "邮箱格式不正确,请检查!";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.hanserwei.framework.common.validate;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class EmailValidator implements ConstraintValidator<EmailNumber, String> {
|
||||
|
||||
@Override
|
||||
public void initialize(EmailNumber constraintAnnotation) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String emailNumber, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return emailNumber != null && emailNumber.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user