当前位置:网站首页>User defined annotation verification API parameter phone number
User defined annotation verification API parameter phone number
2022-07-25 15:28:00 【Don't stop laughing】
annotation
Before creating annotations , Let's first clarify the logic of phone number verification , To achieve :
- Mobile number cannot be empty
- Regular expression that conforms to the phone number
- Check the length of the phone number
Create an interface
@Documented
@Constraint(validatedBy = PhoneNumberValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface PhoneNumber {
String message() default "Invalid phone number";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}Logic implementation
public class PhoneNumberValidator implements ConstraintValidator<PhoneNumber, String> {
/**
* Implements the validation logic.
* The state of {@code value} must not be altered.
* <p>
* This method can be accessed concurrently, thread-safety must be ensured
* by the implementation.
*
* @param value object to validate
* @param context context in which the constraint is evaluated
*
* @return {@code false} if {@code value} does not pass the constraint
*/
@Override
public boolean isValid(String phoneField, ConstraintValidatorContext context) {
// can be null
if (phoneField == null) return true;
return phoneField != null && phoneField.matches("[0-9]+")
&& (phoneField.length() > 8) && (phoneField.length() < 14);
}
}
边栏推荐
- 记一次Spark报错:Failed to allocate a page (67108864 bytes), try again.
- 自定义注解校验API参数电话号
- Xcode添加mobileprovision证书文件报错:Xcode encountered an error
- Simulate setinterval timer with setTimeout
- ML - natural language processing - Key Technologies
- Image cropper example
- C#精挑整理知识要点11 委托和事件(建议收藏)
- Idea eye care settings
- 在网页上实现任意格式的音视频快速播放功能的开发总结。
- MySQL transactions and mvcc
猜你喜欢

伤透脑筋的CPU 上下文切换

ML - natural language processing - Key Technologies

Yan required executor memory is above the max threshold (8192mb) of this cluster!

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28

ML - 语音 - 深度神经网络模型

Spark submission parameters -- use of files

带你创建你的第一个C#程序(建议收藏)

How to solve the login problem after the 30 day experience period of visual stuido2019

使用cpolar建立一个商业网站(如何购买域名)

ML - 自然语言处理 - 关键技术
随机推荐
Outline and box shadow to achieve the highlight effect of contour fillet
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
Idea护眼色设置
How to solve the login problem after the 30 day experience period of visual stuido2019
Spark DF adds a column
NPM's nexus private server e401 E500 error handling record
JVM-动态字节码技术详解
ML - 图像 - 深度学习和卷积神经网络
ML - natural language processing - Key Technologies
pageHelper不生效,sql没有自动加上limit
MATLAB 如何生产随机复序列
p4552-差分
wait()和sleep()的区别理解
自定义注解校验API参数电话号
死锁杂谈
MySQL installation and configuration super detailed tutorial and simple database and table building method
Node learning
Simulate setinterval timer with setTimeout
redis淘汰策列
你准备好脱离“内卷化怪圈”了吗?