大陆港澳台身份证正则验证

大陆港澳台身份证正则验证

  public static boolean getisIdCard(String str) {

        //1.大陆 18 位   可含X
        String Dalu = "([1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx])|([1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3})";
        Pattern p = Pattern.compile(Dalu);
        Matcher m = p.matcher(str);
        boolean matches = m.matches();

        //香港8位  例:  X354670(A)      
        String hongKong = "[A-Za-z]{1}\\d{6}[(\\d)|A]{3}";
        Pattern p2 = Pattern.compile(hongKong);
        Matcher m2 = p2.matcher(str);
        boolean matches2 = m2.matches();

        //台湾10位 例: U193683453  
        String taiWan ="[A-Z]{1}\\d{9}";
        Pattern p3 = Pattern.compile(taiWan);
        Matcher m3 = p3.matcher(str);
        boolean matches3 = m3.matches();


        //澳门8位 例:5686611(1)  
        String maCao = "[1|5|7]\\d{6}[(\\d)]{3}";
        Pattern p1 = Pattern.compile(maCao);
        Matcher m1 = p1.matcher(str);
        boolean matches1 = m1.matches();

        if(matches||matches1||matches2||matches3){
            return true;
        }


        return false;
    }

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Sure, 我可以帮你解决这个问题。身份证号码校验程序的实现可以通过以下步骤完成: 1. 在Java中创建一个新的类,使用Scanner类获取用户输入的身份证号码。 2. 创建一个方法,该方法将输入的身份证号码作为参数,并将其转换为char[]数组。 3. 对转换后的数组进行一些简单的验证,例如检查长度是否为18个字符以及前17个字符是否都是数字。 4. 使用第17个和第18个字符计算身份证号码的校验和。 5. 将ComputeCheckSum方法引用到ValidateID方法中,以计算并验证身份证号码的校验和。 6. 加入对地区代码和出生日期的验证。 以下是身份证号码校验程序的示例代码: ``` import java.util.Scanner; public class ValidateID { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your ID number: "); String id = scanner.next(); if (ValidateID(id)) { System.out.println("Valid ID number"); } else { System.out.println("Invalid ID number"); } } public static boolean ValidateID(String id) { if (id == null || id.trim().length() != 18) { return false; } char[] ids = id.toCharArray(); for (int i = 0; i < ids.length - 1; i++) { if (!Character.isDigit(ids[i])) { return false; } } String[] wf = new String[]{"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"}; String[] vcode = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; int sum = 0; for (int i = 0; i < wf.length; i++) { sum += Integer.parseInt(wf[i]) * Integer.parseInt(String.valueOf(ids[i])); } int mod = sum % 11; String last = String.valueOf(ids[17]); if (mod == 2) { if (!last.equalsIgnoreCase("x")) { return false; } } else { if (!last.equals(vcode[mod])) { return false; } } String areaCode = id.substring(0, 6); String year = id.substring(6, 10); String month = id.substring(10, 12); String day = id.substring(12, 14); String birthday = year + "-" + month + "-" + day; boolean yearCheck = Integer.valueOf(year) >= 1900 && Integer.valueOf(year) <= (Calendar.getInstance().get(Calendar.YEAR) - 18); try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); format.setLenient(false); format.parse(birthday); } catch (Exception e) { return false; } boolean areaCheck = false; for (String area : areaCodeArray) { if (area.equals(areaCode.substring(0, 2))) { areaCheck = true; break; } } return yearCheck && areaCheck; } } ``` 请注意,我并不是储存已经输入的文本,所以我将无法记住之前提到的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值