java中检查用户输入信息是否正确?

 时间:2026-02-13 19:53:58

1、电话号码检查代码如下:

public static boolean isPhoneByPattern(String phone) {  

    String regex = "^1[3|4|5|7|8]\\d{9}$";  

    Pattern pattern = Pattern.compile(regex);  

    Matcher matcher = pattern.matcher(phone);  

    return matcher.matches();  

}  

public static boolean isPhoneByString(String phone) {  

    String regex = "[1][3578]\\d{9}";  

    return phone.matches(regex);  

}  

2、邮箱检查:

public static boolean isEmailByPattern(String email) {  

    String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";  

    Pattern pattern = Pattern.compile(regex);  

    Matcher matcher = pattern.matcher(email);  

    return matcher.matches();  

}  

  

public static boolean isEmailByString(String email) {  

    String regex = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)";  

    return email.matches(regex);  

}  

3、身份证检查:

public static boolean isICNOByPattern(String icno) {  

    String regex15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";  

    Pattern pattern15 = Pattern.compile(regex15);  

    Matcher matcher15 = pattern15.matcher(icno);  

    String regex18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)$";  

    Pattern pattern18 = Pattern.compile(regex18);  

    Matcher matcher18 = pattern18.matcher(icno);  

    return (matcher15.matches() || matcher18.matches());  

}  

  

public static boolean isICNOByString(String icno) {  

    String regex15 = "[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}";  

    String regex18 = "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)";  

    return (icno.matches(regex15) || icno.matches(regex18));  

}  

  • 私密浏览器怎么设置启动图案密码
  • 学科abc怎么划分#校园分享#
  • 化学实验课上发生意外怎么办?
  • 网络传输编码与解码
  • 电商淘宝商品的定价方法和技巧策略
  • 热门搜索
    深圳旅游攻略 日本旅游价格 出境旅游 开封旅游 庐山旅游 宁夏旅游 长春旅游 晋城旅游 南宁旅游 汉中旅游