//判断证件号码是那种类型的(1是15位,2是18位,3是组织机构代码证,0是无效) private int panduanCard(String str){ String ps15 = "^\\d{15}$"; String ps18 = "^\\d{17}(\\d|X)$"; //组织机构代码证正则 String zzjg = "^[a-zA-Z0-9]{8}-[a-zA-Z0-9]$"; PatternCompiler orocom = new Perl5Compiler(); try { Pattern pattern1 = orocom.compile(ps15); Pattern pattern2 = orocom.compile(ps18); Pattern zzPattern = orocom.compile(zzjg); PatternMatcher matcher = new Perl5Matcher(); if (matcher.contains(str, pattern1)) { return 1; }else if (matcher.contains(str, pattern2)) { return 2; }else if (matcher.contains(str, zzPattern)) { return 3; } } catch (MalformedPatternException e) { e.printStackTrace(); } return 0; }
//判断18位是否为身份证 private boolean isIdCard(String str){ Integer[] rows=new Integer[17]; for (int i = 0; i < rows.length; i++) { rows[i]=Integer.parseInt(str.charAt(i)+""); } //权值 Integer[] quanvalue={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; //校验码 String[] check={"1","0","X","9","8","7","6","5","4","3","2"}; int tot=0; for (int i = 0; i < quanvalue.length; i++) { tot+=rows[i]*quanvalue[i]; } int yushu = tot%11; return check[yushu].equals(str.substring(17, 18).toUpperCase()); }
//15位升为成18位 private String shengwei(String str){ String newString=str.substring(0, 6)+"19"+str.substring(6, str.length()); Integer[] rows=new Integer[17]; for (int i = 0; i < rows.length; i++) { rows[i]=Integer.parseInt(newString.charAt(i)+""); } //权值 Integer[] quanvalue={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; //校验码 String[] check={"1","0","X","9","8","7","6","5","4","3","2"}; int tot=0; for (int i = 0; i < quanvalue.length; i++) { tot+=rows[i]*quanvalue[i]; } int yushu = tot%11; return newString+check[yushu]; }
原文地址:http://blog.csdn.net/baidu_16597357/article/details/42388051