判断是第几代身份证(第一代15位, 第二代18位)

 if (cardId.length() == 15 || cardId.length() == 18) {
if (!this.cardCodeVerifySimple(cardId)) {
error.put("cardId", "15位或18位身份证号码不正确");
} else {
if (cardId.length() == 18 && !this.cardCodeVerify(cardId)) {
error.put("cardId", "18位身份证号码不符合国家规范");
}
}
} else {
error.put("cardId", "身份证号码长度必须等于15或18位");
}

正则校验身份证是否符合第一代第二代标准

 private boolean cardCodeVerifySimple(String cardcode) {
//第一代身份证正则表达式(15位)
String isIDCard1 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
//第二代身份证正则表达式(18位)
String isIDCard2 ="^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$"; //验证身份证
if (cardcode.matches(isIDCard1) || cardcode.matches(isIDCard2)) {
return true;
}
return false;
}

验证第二代身份证是否符合国家规范

 private boolean cardCodeVerify(String cardcode) {
int i = 0;
String r = "error";
String lastnumber = ""; i += Integer.parseInt(cardcode.substring(0, 1)) * 7;
i += Integer.parseInt(cardcode.substring(1, 2)) * 9;
i += Integer.parseInt(cardcode.substring(2, 3)) * 10;
i += Integer.parseInt(cardcode.substring(3, 4)) * 5;
i += Integer.parseInt(cardcode.substring(4, 5)) * 8;
i += Integer.parseInt(cardcode.substring(5, 6)) * 4;
i += Integer.parseInt(cardcode.substring(6, 7)) * 2;
i += Integer.parseInt(cardcode.substring(7, 8)) * 1;
i += Integer.parseInt(cardcode.substring(8, 9)) * 6;
i += Integer.parseInt(cardcode.substring(9, 10)) * 3;
i += Integer.parseInt(cardcode.substring(10,11)) * 7;
i += Integer.parseInt(cardcode.substring(11,12)) * 9;
i += Integer.parseInt(cardcode.substring(12,13)) * 10;
i += Integer.parseInt(cardcode.substring(13,14)) * 5;
i += Integer.parseInt(cardcode.substring(14,15)) * 8;
i += Integer.parseInt(cardcode.substring(15,16)) * 4;
i += Integer.parseInt(cardcode.substring(16,17)) * 2;
i = i % 11;
lastnumber =cardcode.substring(17,18);
if (i == 0) {
r = "1";
}
if (i == 1) {
r = "0";
}
if (i == 2) {
r = "x";
}
if (i == 3) {
r = "9";
}
if (i == 4) {
r = "8";
}
if (i == 5) {
r = "7";
}
if (i == 6) {
r = "6";
}
if (i == 7) {
r = "5";
}
if (i == 8) {
r = "4";
}
if (i == 9) {
r = "3";
}
if (i == 10) {
r = "2";
}
if (r.equals(lastnumber.toLowerCase())) {
return true;
}
return false;
}

身份证校验(java)的更多相关文章

  1. java实现身份证校验

    原文来自:java教程网 题目:身份证校验   身份证校验   如果让你设计个程序,用什么变量保存身份证号码呢?长整数可以吗?不可以!   因为有人的身份证最后一位是"X"   实 ...

  2. Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码正则表达式

    Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码校验正则表达式 ============================== ©Copyright 蕃薯耀 2017年11 ...

  3. Java实现18位身份证校验代码

    import java.util.Scanner; /** * 18位身份证校验 * @author [J.H] * */ public class Test { // 身份证校验 public st ...

  4. 篇二:JS身份证校验

    身份证校验 function identityCodeValid(code) { var city={11:"北京",12:"天津",13:"河北&q ...

  5. Powerbuilder编写身份证校验码

    public function boolean of_calc_cardid_verifycode (string as_cardid, ref string as_verifycode); /* 计 ...

  6. 最严格的身份证校验(JavaScript版)

    在JavaWeb研发过程中为了获取有效的用户信息,校验其数据的有效性非常是必要,以下贴出在项目中用到的关于身份证的校验: <!DOCTYPE HTML PUBLIC "-//W3C// ...

  7. 身份证校验程序(下)- 零基础入门学习Delphi49

    身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...

  8. 身份证校验程序(上)- 零基础入门学习Delphi48

    身份证校验程序 让编程改变世界 Change the world by program [caption id="attachment_2699" align="alig ...

  9. 完美实现身份证校验 js正则

    注意: 1.只针对18为身份证号码进行校验,现在15位的应该很少了, 2.不区分xX大小写, 3.出生年份1900-2099,每月的天数也进行相关验证(考虑的闰月的情况), 4.校验规则详见,这个写的 ...

随机推荐

  1. win8.1安装Team Function Server 2013

    1.系统是Win8.1企业版64位 2.Visual Studio 2013 3.数据库是Sql Server2012 (这里注意需要升级到SP1否则安装可能失败下载地址http://www.micr ...

  2. Firemonkey 在 iOS 平台能显示更多的 emoji 字符

    使用 Firmonkey 在显示 emoji 字符时,有些 emoji 并无法显示彩色,见下图: 经查 FMX 源码,是因为判断 emoji 的字符区段不足造成的,经过修改后,便可显示,见下图: 修改 ...

  3. 【转】php中XML、XSLT的结合运用

    原文:http://blog.csdn.net/bjbs_270/article/details/140253   下面我要讲的是一个简单的从数据库中抽取数据,生成XML文档,使用XSLT转换成HTM ...

  4. structs常见错误

    原因:打开struts-default.xml文件 解决办法: 重启Myeclipse

  5. linux常用命令之文件系统

    df df - report file system disk space usage 查看文件系统的使用清空 用法 df [-hi] [path] 选项 -h human readable ,以人类 ...

  6. jScrollPane 美化滚动条

    在线实例 滚动条可见 滚动条隐藏 使用方法 <div class="container"> <h1>滚动条可见</h1> <div cla ...

  7. css选择器中:first-child与:first-of-type的区别

    :first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素.比如有段代码: p:first-child  匹配到的是p元素,因为p元素是div的第一个子元素: ...

  8. css小技巧,会不断更新的

    1.去除input记住密码后自动填充表单的黄色背景 input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #FFF inset; }/ ...

  9. Dotfuscator混淆加密

    混淆加密 1. 需要安装Dotfuscator软件 2. 安装好后打开软件,找到编译好的DLL文件 3. 打开[setting]设置属性,如下图: 把 Disable String Encryptio ...

  10. 利用Ajax增删改Sharepoint List Item

    在使用一个工具的是想要在本地的HTML文件或者JS,修改Sharepoint List中的数据. 如下是找到的方法.不知道还有其他方法没.IE中可以使用.记得加载Jquery. 如果是Chrome 浏 ...