java算法之身份证号码验证

调用时直接new IDCard().verify(身份证id);就可以了实现代码如下:public class IDCard { private String _codeError; //wi =2(n-1)(mod 11) final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 1

大家好,又见面了,我是全栈君。

调用时直接

new IDCard().verify(身份证id);就可以了

实现代码如下:

public class IDCard {
private String _codeError;
//wi =2(n-1)(mod 11)
final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
// verify digit
final int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
private int[] ai = new int[18];
private static String[] _areaCode={"11","12","13","14","15","21","22"
,"23","31","32","33","34","35","36","37","41","42","43","44"
,"45","46","50","51","52","53","54","61","62","63","64","65","71","81","82","91"};
private static HashMap<String,Integer> dateMap;
private static HashMap<String,String> areaCodeMap;
static{
dateMap=new HashMap<String,Integer>();
dateMap.put("01",31);
dateMap.put("02",null);
dateMap.put("03",31);
dateMap.put("04",30);
dateMap.put("05",31);
dateMap.put("06",30);
dateMap.put("07",31);
dateMap.put("08",31);
dateMap.put("09",30);
dateMap.put("10",31);
dateMap.put("11",30);
dateMap.put("12",31);
areaCodeMap=new HashMap<String,String>();
for(String code:_areaCode){
areaCodeMap.put(code,null);
}
}
//验证身份证位数,15位和18位身份证
public boolean verifyLength(String code){
int length=code.length();
if(length==15 || length==18){
return true;
}else{
_codeError="错误:输入的身份证号不是15位和18位的";
return false;
}
}
//判断地区码
public boolean verifyAreaCode(String code){
String areaCode=code.substring(0,2);
//            Element child=  _areaCodeElement.getChild("_"+areaCode);
if(areaCodeMap.containsKey(areaCode)){
return true;
}else{
_codeError="错误:输入的身份证号的地区码(1-2位)["+areaCode+"]不符合中国行政区划分代码规定(GB/T2260-1999)";
return false;
}
}
//判断月份和日期
public boolean verifyBirthdayCode(String code){
//验证月份
String month=code.substring(10,12);
boolean isEighteenCode=(18==code.length());
if(!dateMap.containsKey(month)){
_codeError="错误:输入的身份证号"+(isEighteenCode?"(11-12位)":"(9-10位)")+"不存在["+month+"]月份,不符合要求(GB/T7408)";
return false;
}
//验证日期
String dayCode=code.substring(12,14);
Integer day=dateMap.get(month);
String yearCode=code.substring(6,10);
Integer year=Integer.valueOf(yearCode);
//非2月的情况
if(day!=null){
if(Integer.valueOf(dayCode)>day || Integer.valueOf(dayCode)<1){
_codeError="错误:输入的身份证号"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]号不符合小月1-30天大月1-31天的规定(GB/T7408)";
return false;
}
}
//2月的情况
else{
//闰月的情况
if((year%4==0&&year%100!=0)||(year%400==0)){
if(Integer.valueOf(dayCode)>29 || Integer.valueOf(dayCode)<1){
_codeError="错误:输入的身份证号"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]号在"+year+"闰年的情况下未符合1-29号的规定(GB/T7408)";
return false;
}
}
//非闰月的情况
else{
if (Integer.valueOf(dayCode) > 28 || Integer.valueOf(dayCode) < 1) {
_codeError="错误:输入的身份证号"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]号在"+year+"平年的情况下未符合1-28号的规定(GB/T7408)";
return false;
}
}
}
return true;
}
//验证身份除了最后位其他的是否包含字母
public boolean containsAllNumber(String code) {
String str="";
if(code.length()==15){
str=code.substring(0,15);
}else if(code.length()==18){
str=code.substring(0,17);
}
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (! (ch[i] >= '0' && ch[i] <= '9')) {
_codeError="错误:输入的身份证号第"+(i+1)+"位包含字母";
return false;
}
}
return true;
}
public String getCodeError(){
return _codeError;
}
//验证身份证
public boolean verify(String idcard) {
_codeError="";
//验证身份证位数,15位和18位身份证
if(!verifyLength(idcard)){
return false;
}
//验证身份除了最后位其他的是否包含字母
if(!containsAllNumber(idcard)){
return false;
}
//如果是15位的就转成18位的身份证
String eifhteencard="";
if (idcard.length() == 15) {
eifhteencard = uptoeighteen(idcard);
}else{
eifhteencard=idcard;
}
//验证身份证的地区码
if(!verifyAreaCode(eifhteencard)){
return false;
}
//判断月份和日期
if(!verifyBirthdayCode(eifhteencard)){
return false;
}
//验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
if(!verifyMOD(eifhteencard)){
return false;
}
return true;
}
//验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
public boolean verifyMOD(String code){
String verify = code.substring(17, 18);
if("x".equals(verify)){
code=code.replaceAll("x","X");
verify="X";
}
String verifyIndex=getVerify(code);
if (verify.equals(verifyIndex)) {
return true;
}
//            int x=17;
//            if(code.length()==15){
//                  x=14;
//            }
_codeError="错误:输入的身份证号最末尾的数字验证码错误";
return false;
}
//获得校验位
public String getVerify(String eightcardid) {
int remaining = 0;
if (eightcardid.length() == 18) {
eightcardid = eightcardid.substring(0, 17);
}
if (eightcardid.length() == 17) {
int sum = 0;
for (int i = 0; i < 17; i++) {
String k = eightcardid.substring(i, i + 1);
ai[i] = Integer.parseInt(k);
}
for (int i = 0; i < 17; i++) {
sum = sum + wi[i] * ai[i];
}
remaining = sum % 11;
}
return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
}
//15位转18位身份证
public String uptoeighteen(String fifteencardid) {
String eightcardid = fifteencardid.substring(0, 6);
eightcardid = eightcardid + "19";
eightcardid = eightcardid + fifteencardid.substring(6, 15);
eightcardid = eightcardid + getVerify(eightcardid);
return eightcardid;
}

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121404.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • CM12同步源码及编译教程

    CM12同步源码及编译教程同时提供基于安卓5.0的MKL魔趣猪扒饭编译教程~【玩机组出品】魔趣猪扒饭MKL50.1编译教程http://www.oneplusbbs.com/forum.php?mod=viewthread&tid=578692环境搭建1.安装64位Ubuntu系统(实体安装、虚拟机安装均可)注意:要求机器至少4G内存(虚拟机至少分配4G内存),硬盘至少100G空间(源码20G+…

  • Android中的跨进程通信方法实例及特点分析(二):ContentProvider

    Android中的跨进程通信方法实例及特点分析(二):ContentProvider

  • hdu 1215 七夕节

    hdu 1215 七夕节

  • idea社区版_idea社区版够用吗

    idea社区版_idea社区版够用吗IDEA官网地址:https://www.jetbrains.com/idea/download/#section=windows下载社区版后,点击安装,就进行傻瓜式的安装了。以上两个步骤中有一个点击next的时候时间会稍稍有点久,耐心等待一下就好了。点击安装,IDEA社区版就安装完成了,安装好之后打开IDEA工具,会有如下提示:我是第一次使用,直接选择Donotimportsettings就行了。到这里就进入IDEA工具了,选择你喜欢的主题风格,当然这还不算正式的完成,我

  • Linux开发在中国[通俗易懂]

    Linux开发在中国[通俗易懂][2004-5-15]  中关村最堵车的白颐路旁的一座公寓里,赵宇一手拿着遥控器,一手指着电视屏幕给客户演示着流媒体的点播效果。作为腾博讯公司的总经理,赵宇已经在Linux圈里面摸爬滚打了五年多。他曾策划发行过Linux光盘,创建过Linux社区,做过Linux杂志主编。现在,他又基于Linux流媒体服务器技术创建了自己的公司。  但…

  • python 函数嵌套和nonlocal关键字

    python 函数嵌套和nonlocal关键字python函数可以嵌套使用,使用也比较简单,举个栗子:defouter():print("outer")definner():print("inner")inner()outer()##运行结果:outerinnernonlocal关键字:与global关键字有点相似,可以对比着理解。nonlocal关键字只能作用域局…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号