热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android身份证号有效性校验工具类案例

这篇文章主要介绍了Android身份证号有效性校验工具类案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

不记得从哪找的了,修改了部分代码,修复在Android平台下使用时,时区时间格式异常的问题。

package cn.aikongmeng.demo.utils;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by Arjun on 2017/4/25.
 * 身份证有效性校验
 */

public class IdentityUtils {
  //  位权值数组
  private static byte[] Wi = new byte[17];
  //  身份证前部分字符数
  private static final byte fPart = 6;
  //  身份证算法求模关键值
  private static final byte fMod = 11;
  //  旧身份证长度
  private static final byte oldIDLen = 15;
  //  新身份证长度
  private static final byte newIDLen = 18;
  //  新身份证年份标志
  private static final String yearFlag = "19";
  //  校验码串
  private static final String CheckCode = "10X98765432";
  //  最小的行政区划码
  private static final int minCode = 150000;
  //  最大的行政区划码
  private static final int maxCode = 700000;
//  旧身份证号码
//  private String oldIDCard="";
//  新身份证号码
//  private String newIDCard="";
//  地区及编码

  //private String Area[][2] =
  private static void setWiBuffer() {
    for (int i = 0; i 

补充知识:Android 【身份证校验方法】已封装 可以直接调用 可用

做项目的时候,有需要对用户进行身份证进行校验,苦于单独写一个比较麻烦,于是找度娘协助,试了几个方法这个方法比较全面一些,比较实用。

记录下这个,也感谢原作者,只是现在忘了在哪复制了

import android.text.TextUtils; 
/**
 * 身份证的工具类
 */
public class IdCardUtil {
 
  private String idCardNum = null; 
  private static int IS_EMPTY = 1;
  private static int LEN_ERROR = 2;
  private static int CHAR_ERROR = 3;
  private static int DATE_ERROR = 4;
  private static int CHECK_BIT_ERROR = 5; 
  private String[] errMsg = new String[]{"身份证完全正确!",
      "身份证为空!",
      "身份证长度不正确!",
      "身份证有非法字符!",
      "身份证中出生日期不合法!",
      "身份证校验位错误!"};
 
  private int error = 0;
 
  /**
   * 构造方法。
   *
   * @param idCardNum
   */
  public IdCardUtil(String idCardNum) {
    // super();
    this.idCardNum = idCardNum.trim();
    if (!TextUtils.isEmpty(this.idCardNum)) {
      this.idCardNum = this.idCardNum.replace("x", "X");
    }
  }
 
  public String getIdCardNum() {
    return idCardNum;
  }
 
  public void setIdCardNum(String idCardNum) {
    this.idCardNum = idCardNum;
    if (!TextUtils.isEmpty(this.idCardNum)) {
      this.idCardNum = this.idCardNum.replace("x", "X");
    }
  }
 
  /**
   * 得到身份证详细错误信息。
   *
   * @return 错误信息。
   */
  public String getErrMsg() {
    return this.errMsg[this.error];
  }
 
  /**
   * 是否为空。
   *
   * @return true: null false: not null;
   */
  public boolean isEmpty() {
    if (this.idCardNum == null)
      return true;
    else
      return this.idCardNum.trim().length() > 0 ? false : true;
  }
 
  /**
   * 身份证长度。
   *
   * @return
   */
  public int getLength() {
    return this.isEmpty() ? 0 : this.idCardNum.length();
  }
 
  /**
   * 身份证长度。
   *
   * @return
   */
  public int getLength(String str) {
    return this.isEmpty() ? 0 : str.length();
  }
 
  /**
   * 是否是15位身份证。
   *
   * @return true: 15位 false:其他。
   */
  public boolean is15() {
    return this.getLength() == 15;
  }
 
  /**
   * 是否是18位身份证。
   *
   * @return true: 18位 false:其他。
   */
  public boolean is18() {
    return this.getLength() == 18;
  }
 
  /**
   * 得到身份证的省份代码。
   *
   * @return 省份代码。
   */
  public String getProvince() {
    return this.isCorrect() == 0 ? this.idCardNum.substring(0, 2) : "";
  }
 
  /**
   * 得到身份证的城市代码。
   *
   * @return 城市代码。
   */
  public String getCity() {
    return this.isCorrect() == 0 ? this.idCardNum.substring(2, 4) : "";
  }
 
  /**
   * 得到身份证的区县代码。
   *
   * @return 区县代码。
   */
  public String getCountry() {
    return this.isCorrect() == 0 ? this.idCardNum.substring(4, 6) : "";
  }
 
  /**
   * 得到身份证的出生年份。
   *
   * @return 出生年份。
   */
  public String getYear() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.getLength() == 15) {
      return "19" + this.idCardNum.substring(6, 8);
    } else {
      return this.idCardNum.substring(6, 10);
    }
  }
 
  /**
   * 得到身份证的出生月份。
   *
   * @return 出生月份。
   */
  public String getMonth() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.getLength() == 15) {
      return this.idCardNum.substring(8, 10);
    } else {
      return this.idCardNum.substring(10, 12);
    }
  }
 
  /**
   * 得到身份证的出生日子。
   *
   * @return 出生日期。
   */
  public String getDay() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.getLength() == 15) {
      return this.idCardNum.substring(10, 12);
    } else {
      return this.idCardNum.substring(12, 14);
    }
  }
 
  /**
   * 得到身份证的出生日期。
   *
   * @return 出生日期。
   */
  public String getBirthday() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.getLength() == 15) {
      return "19" + this.idCardNum.substring(6, 12);
    } else {
      return this.idCardNum.substring(6, 14);
    }
  }
 
  /**
   * 得到身份证的出生年月。
   *
   * @return 出生年月。
   */
  public String getBirthMonth() {
    return getBirthday().substring(0, 6);
  }
 
  /**
   * 得到身份证的顺序号。
   *
   * @return 顺序号。
   */
  public String getOrder() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.getLength() == 15) {
      return this.idCardNum.substring(12, 15);
    } else {
      return this.idCardNum.substring(14, 17);
    }
  }
 
  /**
   * 得到性别。
   *
   * @return 性别:1-男 2-女
   */
  public String getSex() {
    if (this.isCorrect() != 0)
      return "";
 
    int p = Integer.parseInt(getOrder());
    if (p % 2 == 1) {
      return "男";
    } else {
      return "女";
    }
  }
 
  /**
   * 得到性别值。
   *
   * @return 性别:1-男 2-女
   */
  public String getSexValue() {
    if (this.isCorrect() != 0)
      return "";
 
    int p = Integer.parseInt(getOrder());
    if (p % 2 == 1) {
      return "1";
    } else {
      return "2";
    }
  }
 
  /**
   * 得到校验位。
   *
   * @return 校验位。
   */
  public String getCheck() {
    if (!this.isLenCorrect())
      return "";
 
    String lastStr = this.idCardNum.substring(this.idCardNum.length() - 1);
    if ("x".equals(lastStr)) {
      lastStr = "X";
    }
    return lastStr;
  }
 
  /**
   * 得到15位身份证。
   *
   * @return 15位身份证。
   */
  public String to15() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.is15())
      return this.idCardNum;
    else
      return this.idCardNum.substring(0, 6) + this.idCardNum.substring(8, 17);
  }
 
  /**
   * 得到18位身份证。
   *
   * @return 18位身份证。
   */
  public String to18() {
    if (this.isCorrect() != 0)
      return "";
 
    if (this.is18())
      return this.idCardNum;
    else
      return this.idCardNum.substring(0, 6) + "19" + this.idCardNum.substring(6) + this.getCheckBit();
  }
 
  /**
   * 得到18位身份证。
   *
   * @return 18位身份证。
   */
  public static String toNewIdCard(String tempStr) {
    if (tempStr.length() == 18)
      return tempStr.substring(0, 6) + tempStr.substring(8, 17);
    else
      return tempStr.substring(0, 6) + "19" + tempStr.substring(6) + getCheckBit(tempStr);
  } 
  /**
   * 校验身份证是否正确
   *
   * @return 0:正确
   */
  public int isCorrect() {
    if (this.isEmpty()) {
      this.error = IdCardUtil.IS_EMPTY;
      return this.error;
    }
 
    if (!this.isLenCorrect()) {
      this.error = IdCardUtil.LEN_ERROR;
      return this.error;
    }
 
    if (!this.isCharCorrect()) {
      this.error = IdCardUtil.CHAR_ERROR;
      return this.error;
    }
 
    if (!this.isDateCorrect()) {
      this.error = IdCardUtil.DATE_ERROR;
      return this.error;
    }
 
    if (this.is18()) {
      if (!this.getCheck().equals(this.getCheckBit())) {
        this.error = IdCardUtil.CHECK_BIT_ERROR;
        return this.error;
      }
    } 
    return 0;
  } 
 
  private boolean isLenCorrect() {
    return this.is15() || this.is18();
  }
 
  /**
   * 判断身份证中出生日期是否正确。
   *
   * @return
   */
  private boolean isDateCorrect() {
 
    /*非闰年天数*/
    int[] mOnthDayN= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    /*闰年天数*/
    int[] mOnthDayL= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
    int month;
    if (this.is15()) {
      mOnth= Integer.parseInt(this.idCardNum.substring(8, 10));
    } else {
      mOnth= Integer.parseInt(this.idCardNum.substring(10, 12));
    }
 
    int day;
    if (this.is15()) {
      day = Integer.parseInt(this.idCardNum.substring(10, 12));
    } else {
      day = Integer.parseInt(this.idCardNum.substring(12, 14));
    }
 
    if (month > 12 || month <= 0) {
      return false;
    }
 
    if (this.isLeapyear()) {
      if (day > monthDayL[month - 1] || day <= 0)
        return false;
    } else {
      if (day > monthDayN[month - 1] || day <= 0)
        return false;
    }
 
    return true;
  }
 
  /**
   * 得到校验位。
   *
   * @return
   */
  private String getCheckBit() {
    if (!this.isLenCorrect())
      return "";
 
    String temp = null;
    if (this.is18())
      temp = this.idCardNum;
    else
      temp = this.idCardNum.substring(0, 6) + "19" + this.idCardNum.substring(6); 
 
    String checkTable[] = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
    int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    int sum = 0;
 
    for (int i = 0; i <17; i++) {
      String ch = temp.substring(i, i + 1);
      sum = sum + Integer.parseInt(ch) * wi[i];
    } 
    int y = sum % 11; 
    return checkTable[y];
  } 
 
  /**
   * 得到校验位。
   *
   * @return
   */
  private static String getCheckBit(String str) {
 
    String temp = null;
    if (str.length() == 18)
      temp = str;
    else
      temp = str.substring(0, 6) + "19" + str.substring(6); 
 
    String checkTable[] = new String[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
    int[] wi = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
    int sum = 0;
 
    for (int i = 0; i <17; i++) {
      String ch = temp.substring(i, i + 1);
      sum = sum + Integer.parseInt(ch) * wi[i];
    } 
    int y = sum % 11; 
    return checkTable[y];
  } 
 
  /**
   * 身份证号码中是否存在非法字符。
   *
   * @return true: 正确 false:存在非法字符。
   */
  private boolean isCharCorrect() {
    boolean iRet = true;
 
    if (this.isLenCorrect()) {
      byte[] temp = this.idCardNum.getBytes();
 
      if (this.is15()) {
        for (int i = 0; i  57) {
            iRet = false;
            break;
          }
        }
      }
 
      if (this.is18()) {
        for (int i = 0; i  57) {
            if (i == 17 && temp[i] != 88) {
              iRet = false;
              break;
            }
          }
        }
      }
    } else {
      iRet = false;
    }
    return iRet;
  }
 
  /**
   * 判断身份证的出生年份是否未闰年。
   *
   * @return true :闰年 false 平年
   */
  private boolean isLeapyear() {
    String temp;
 
    if (this.is15()) {
      temp = "19" + this.idCardNum.substring(6, 8);
    } else {
      temp = this.idCardNum.substring(6, 10);
    }
 
    int year = Integer.parseInt(temp);
 
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
      return true;
    else
      return false;
  }
}

重要的是这个方法可以验证"X","x"。

以上这篇Android身份证号有效性校验工具类案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


推荐阅读
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • Objective C接入Sonar代码扫描
    目录技术方案环境准备扫描器配置项目配置SonarQube配置jenkins接入一些坑技术方案Sonar本身有对OC的代码扫描插件——SonarCFamily,但是是收费的。出于成本 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 解决Cydia数据库错误:could not open file /var/lib/dpkg/status 的方法
    本文介绍了解决iOS系统中Cydia数据库错误的方法。通过使用苹果电脑上的Impactor工具和NewTerm软件,以及ifunbox工具和终端命令,可以解决该问题。具体步骤包括下载所需工具、连接手机到电脑、安装NewTerm、下载ifunbox并注册Dropbox账号、下载并解压lib.zip文件、将lib文件夹拖入Books文件夹中,并将lib文件夹拷贝到/var/目录下。以上方法适用于已经越狱且出现Cydia数据库错误的iPhone手机。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • imnewtotheswiftandxcodeworld,soimhavingaproblemtryingtointegrateapackagetomypro ... [详细]
  • iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
    从2月14日开始,上传程序的同学可能会遇到提示上传失败的提示.并且打开自己的钥匙串,发现所有的证书全部都显示此证书签发者无效.出现以下情况:Failedtolocateorgene ... [详细]
  • 眼下准备入手MacbookAir的8GBRam版。 ... [详细]
author-avatar
涉世未深的phper
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有