手机号码的验证
public class Validator {
// 验证手机号
public static boolean isValidPhoneNumber(String phoneNumber) {
String regex = "^1[3-9]\\d{9}$"; // 我国手机号
return phoneNumber.matches(regex);
}
// 验证电话号码
public static boolean isValidTelephone(String telephone) {
String regex = "^(\\d{3,4}-)?\\d{7,8}$"; // 区号可选,后面是电话号码
return telephone.matches(regex);
}
// 验证邮箱地址
public static boolean isValidEmail(String email) {
String regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}$"; // 邮箱格式
return email.matches(regex);
}
public static void main(String[] args) {
// 测试数据
String mobile = "13812345678";
String tel = "021-12345678";
String email = "example@test.com";
// 验证并输出结果
System.out.println("手机号验证: " + isValidPhoneNumber(mobile));
System.out.println("电话号码验证: " + isValidTelephone(tel));
System.out.println("邮箱验证: " + isValidEmail(email));
}
}
原文地址:https://blog.csdn.net/m0_37346206/article/details/143886540
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!