本文目录导读:

- 使用正则表达式(最常用)
- 支持国际手机号(使用Google libphonenumber)
- 使用Jakarta Bean Validation(@Pattern注解)
- 增强版正则(支持更多格式)
- 推荐使用哪种方式?
在Java中校验手机号格式,最常用的方式有正则表达式、Apache Commons Validator或Google libphonenumber,以下是几种常见的实现方法和案例:
使用正则表达式(最常用)
public class PhoneValidator {
// 中国大陆手机号正则:1开头的11位数字,第二位通常是3-9
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
public static boolean isValidPhone(String phone) {
if (phone == null || phone.isEmpty()) {
return false;
}
return phone.matches(PHONE_REGEX);
}
public static void main(String[] args) {
String[] testPhones = {
"13812345678", // 有效
"15912345678", // 有效
"10012345678", // 无效(第二位是0)
"1381234567", // 无效(10位)
"138123456789", // 无效(12位)
"abc12345678", // 无效(含字母)
null, // 无效(null)
"" // 无效(空)
};
for (String phone : testPhones) {
System.out.println(phone + " -> " + isValidPhone(phone));
}
}
}
输出:
13812345678 -> true
15912345678 -> true
10012345678 -> false
1381234567 -> false
138123456789 -> false
abc12345678 -> false
null -> false
-> false
支持国际手机号(使用Google libphonenumber)
添加依赖(Maven)
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>8.13.42</version>
</dependency>
校验示例
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
public class InternationalPhoneValidator {
public static boolean isValidPhone(String phone, String countryCode) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber number = phoneUtil.parse(phone, countryCode);
return phoneUtil.isValidNumber(number);
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
// 校验中国大陆手机号
System.out.println(isValidPhone("13812345678", "CN")); // true
System.out.println(isValidPhone("10012345678", "CN")); // false (无效号码)
// 校验美国手机号
System.out.println(isValidPhone("+1 212-456-7890", "US")); // true
System.out.println(isValidPhone("1234567890", "US")); // true (10位数字)
// 自动检测国家代码
System.out.println(isValidPhone("+8613812345678", null)); // true
}
}
使用Jakarta Bean Validation(@Pattern注解)
如果你的项目使用Spring Boot或Jakarta EE,可以在实体类中直接使用注解:
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
实体类
import jakarta.validation.constraints.Pattern;
public class User {
@Pattern(
regexp = "^1[3-9]\\d{9}$",
message = "手机号格式不正确"
)
private String phone;
// getter 和 setter
}
在Controller中使用
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
@RestController
public class UserController {
@PostMapping("/user")
public String createUser(@Valid @RequestBody User user) {
return "用户创建成功!手机号:" + user.getPhone();
}
}
增强版正则(支持更多格式)
public class EnhancedPhoneValidator {
// 支持:+86开头、有/无分隔符(-、空格)
private static final String FLEXIBLE_PHONE_REGEX =
"^(\\+?86)?[ -]?1[3-9]\\d{1}[ -]?\\d{4}[ -]?\\d{4}$";
public static boolean isValidPhoneFlexible(String phone) {
if (phone == null || phone.isEmpty()) {
return false;
}
return phone.matches(FLEXIBLE_PHONE_REGEX);
}
public static void main(String[] args) {
String[] testPhones = {
"13812345678", // true
"+8613812345678", // true
"86 138 1234 5678", // true
"138-1234-5678", // true
"+86-138-1234-5678" // true
};
for (String phone : testPhones) {
System.out.println(phone + " -> " + isValidPhoneFlexible(phone));
}
}
}
推荐使用哪种方式?
| 场景 | 推荐方式 |
|---|---|
| 仅校验中国大陆手机号 | 正则表达式 ^1[3-9]\\d{9}$ |
| 需要国际化支持 | Google libphonenumber |
| Spring Boot项目 | @Pattern注解 + 正则 |
| 前/后端同时校验 | 前端JS正则 + 后端Java正则 |
- 简单场景:直接使用正则
^1[3-9]\\d{9}$ - 生产环境:建议用
libphonenumber,它可以处理号码格式化、有效性验证、国家码识别等复杂场景 - Spring项目:结合
@Pattern注解实现声明式校验
如果需要完整的可运行代码,建议将正则表达式提取为常量,并封装成工具类方便复用。