验证身份证有效性:

2024-05-26 02:18
文章标签 验证 身份证 有效性

本文主要是介绍验证身份证有效性:,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

package com.phonefu.android.help;


import java.util.HashMap;


/**
 * 身份证号校验 
 */
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 = "";
if (idcard == null) {
return false;
}
// 验证身份证位数,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;
}


public static void main(String[] args) {
boolean result = new IdCard().verify("421181198902185817");
System.out.println(result);
}
}

这篇关于验证身份证有效性:的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1003277

相关文章

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo

20170723 做的事 ecdsa的签名验证时间短于bls signature

1 今天在虚拟机 /home/smile/Desktop/20170610/Test//time_ecdsa 文件夹下,找到ecdsa的验证时间是 989.060606μs μs 先 make ,然后run。 再取BLS的签名生成时间: ./run  2  gnuplot 画图,画对比的时间 gnuplot 画图参考教程 http://blog.sciencen

DDS信号的发生器(验证篇)——FPGA学习笔记8

前言:第一部分详细讲解DDS核心框图,还请读者深入阅读第一部分,以便理解DDS核心思想 三刷小梅哥视频总结! 小梅哥https://www.corecourse.com/lander 一、DDS简介         DDS(Direct Digital Synthesizer)即数字合成器,是一种新型的频率合成技术,具有低成本、低功耗、高分辨率、频率转换时间短、相位连续性好等优点,对数字信

Excel实用技巧——二级下拉菜单、数据验证

EXCEL系列文章目录   Excel系列文章是本人亲身经历职场之后萌发的想法,为什么Excel覆盖如此之广,几乎每个公司、学校、家庭都在使用,但是它深藏的宝藏功能却很少被人使用,PQ、BI这些功能同样适用于数据分析;并且在一些需要简单及时的数据分析项目前,Excel是完胜python、R、SPSS这些科学专业的软件的。因此决心开启Excel篇章。 数据分析为什么要学Excel Excel图表

jQuery插件——表单验证(jquery.validate.js)

HTML <form id="loginForm"><label class="form__label--hidden" for="username">用户名:</label><input class="form__input" type="text" id="username" name="username" placeholder="用户名"><label class="form__labe

C语言中的字符输入/输出和验证输入

在C语言中,字符输入/输出功能允许程序与用户进行交互,读取用户的输入信息并展示输出结果。同时,验证输入的作用在于确保用户输入的数据符合预期,以提高程序的稳定性和可靠性,防止无效输入引发的错误或异常行为,从而提供更好的用户体验。 基础概念 输入(Input):指的是向程序填充数据的过程,通常来源于用户输入、文件读取或其他外部数据源。 输出(Output):指的是将数据显示在屏幕上、打印机上或

struts2(三)---struts2中的服务端数据验证框架validate

struts2为我们提供了一个很好的数据验证框架–validate,该框架可以很方便的实现服务端的数据验证。 ActionSupport类提供了一个validate()方法,当我们需要在某一个action中进行数据验证时,可以重写这个方法。数据验证往往是在客户端向服务端提交表单信息时进行的,比如execute方法负责处理表单信息并返回相应的结果,在此之前,validate会先对提交的表单信息进

颠覆多跳事实验证!Causal Walk 前门调整技术引领去偏新纪元

Causal Walk: Debiasing Multi-Hop Fact Verifcation with Front-Door Adjustment 论文地址: Causal Walk: Debiasing Multi-Hop Fact Verification with Front-Door Adjustment| Proceedings of the AAAI Conference

FPGA验证基本内容

FPGA验证的基本内容有:编码规格检查,代码走查,静态时序分析,前仿,动态时序分析,等价性验证,板级验证。

springmvc验证登录用过滤器还是拦截器

https://zhidao.baidu.com/question/650826885570689365.html?qbl=relate_question_0&word=%B9%FD%C2%CB%C6%F7%B5%C7%C2%BD%D1%E9%D6%A4%D3%EB%C0%B9%BD%D8%C6%F7%B5%C7%C2%BD%D1%E9%D6%A4 在struts2中用过filt