Skip to content

Commit fa7af8e

Browse files
committed
feat(validation): 新增 Phone 手机号校验注解,支持校验座机号码、手机号码(中国大陆)、手机号码(中国香港)、手机号码(中国台湾)、手机号码(中国澳门)
区别于 Mobile 手机号校验注解(只校验中国大陆手机号码) Closes #70
1 parent 5ae5b26 commit fa7af8e

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

continew-starter-validation/src/main/java/top/continew/starter/validation/constraints/MobileValidator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
/**
2424
* 手机号校验器
2525
*
26+
* <p>
27+
* 校验中国大陆手机号码
28+
* </p>
29+
*
2630
* @author Charles7c
2731
* @since 2.10.0
2832
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.validation.constraints;
18+
19+
import jakarta.validation.Constraint;
20+
import jakarta.validation.Payload;
21+
22+
import java.lang.annotation.Documented;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
import static java.lang.annotation.ElementType.*;
28+
29+
/**
30+
* 手机号校验注解
31+
*
32+
* <p>
33+
* 校验座机号码、手机号码(中国大陆)、手机号码(中国香港)、手机号码(中国台湾)、手机号码(中国澳门)
34+
* {@code @Phone(message = "手机号格式不正确")} <br />
35+
* </p>
36+
*
37+
* @author Charles7c
38+
* @since 2.13.0
39+
*/
40+
@Documented
41+
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
42+
@Retention(RetentionPolicy.RUNTIME)
43+
@Constraint(validatedBy = PhoneValidator.class)
44+
public @interface Phone {
45+
46+
/**
47+
* 提示消息
48+
*
49+
* @return 提示消息
50+
*/
51+
String message() default "手机号格式不正确";
52+
53+
/**
54+
* 分组
55+
*
56+
* @return 分组
57+
*/
58+
Class<?>[] groups() default {};
59+
60+
/**
61+
* 负载
62+
*
63+
* @return 负载
64+
*/
65+
Class<? extends Payload>[] payload() default {};
66+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.validation.constraints;
18+
19+
import cn.hutool.core.util.PhoneUtil;
20+
import jakarta.validation.ConstraintValidator;
21+
import jakarta.validation.ConstraintValidatorContext;
22+
23+
/**
24+
* 手机号校验器
25+
*
26+
* <p>
27+
* 校验座机号码、手机号码(中国大陆)、手机号码(中国香港)、手机号码(中国台湾)、手机号码(中国澳门)
28+
* </p>
29+
*
30+
* @author Charles7c
31+
* @since 2.13.0
32+
*/
33+
public class PhoneValidator implements ConstraintValidator<Mobile, String> {
34+
35+
@Override
36+
public boolean isValid(String value, ConstraintValidatorContext context) {
37+
if (value == null) {
38+
return true;
39+
}
40+
return PhoneUtil.isPhone(value);
41+
}
42+
}

0 commit comments

Comments
 (0)