转载地址:http://www.w3china.org/blog/more.asp?name=lhwork&id=19148
import java.util.regex.*;
/**
*<p>Title:验证18/15位中国公民身份证号码,并分析有效的数据</p>
*<p>Description:</p>
</p>
*<p>Company:Labile</p>
*@authornotattributable
*@version1.0
*/
publicclass Verifier {
privatestaticfinal String verify18PatternStr = "(\\d{6})" + "(\\d{8})" + "(\\d{3})" + "([\\d[xX]]{1})";
privatestaticfinal Pattern verify18Pattern = Pattern.compile(verify18PatternStr);
privatestaticfinal String verify15PatternStr = "(\\d{6})" + "(\\d{6})" + "(\\d{3})";
privatestaticfinal Pattern verify15Pattern = Pattern.compile(verify15PatternStr);
/**
位的权
*/
privatestaticfinalintverify18Rights[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
/**
*检验码校对表
*/
privatestaticfinal String verify18CheckDigit[] = {"1","0","X","9","8","7","6","5","4","3","2"};
/**
*地区码
*/
private String areaNum;
/**
*出生时间
*/
private String birthdayNum;
/**
*顺序号
*/
private String seqNum;
/**
*校验码
*/
private String check_digit;
privateintlength;
publicboolean isMan() {
int num = Integer.parseInt(seqNum);
if(num%2==1) {
returntrue;
}
else
returnfalse;
}
/**
*得到生日的日期,格式:yyyy-mm-dd
*@returnString
*/
public String getBirthday() {
String FullBirthdayNum = null;
if(length==15)//在生日号码前加“19”
FullBirthdayNum = "19" + birthdayNum;
else
FullBirthdayNum = birthdayNum;
String year = FullBirthdayNum.substring(0, 4);
String month = FullBirthdayNum.substring(4, 6);
String date = FullBirthdayNum.substring(6, 8);
return year + "-" + month + "-" + date;
}
public Verifier(String IDCardNO) throws LengthIllegalException, CheckDigitInvalidException, NumberInvalidException {
if(IDCardNO == null)
thrownew NumberInvalidException();
length = IDCardNO.length();
switch(length) {
case 15:
Verifier15(IDCardNO);
break;
case 18:
Verifier18(IDCardNO);
break;
default:
thrownew LengthIllegalException();
}
}
privatevoid Verifier15(String IDCardNO) throws NumberInvalidException {
Matcher m = verify15Pattern.matcher(IDCardNO);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
areaNum = m.group(1);
birthdayNum = m.group(2);
seqNum = m.group(3);
}
privatevoid Verifier18(String IDCardNO) throws CheckDigitInvalidException, NumberInvalidException {
Matcher m = verify18Pattern.matcher(IDCardNO);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
areaNum = m.group(1);
birthdayNum = m.group(2);
seqNum = m.group(3);
check_digit = m.group(4);
//预期的校验位:
String expect_check_digit = getCheck_digit18(IDCardNO);
//如果校验位无效
if(expect_check_digit.equalsIgnoreCase(check_digit)==false)
thrownew CheckDigitInvalidException();
}
/**
位/17位身份证号算出校验位
*@paramIDCardNOString
*@returnString
*/
publicstatic String getCheck_digit18(String IDCardNO) {
//权总值
int sum = 0;
for(int i=0; i<=16; i++) {
int num = Integer.parseInt( IDCardNO.substring(i,i + 1) );
int right = verify18Rights[i];
sum = sum + num*right;
}
//对权总值取模
int y = sum % 11;
returnverify18CheckDigit[y];
}
publicstatic String IDCardNO15To18(String IDCardNO15) throws
NumberInvalidException {
Matcher m = verify15Pattern.matcher(IDCardNO15);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
String NO17 = IDCardNO15.substring(0,6) + "19" + IDCardNO15.substring(6,15);
return NO17 + getCheck_digit18(NO17);
}
public String getAreaNum() {
returnareaNum;
}
public String getBirthdayNum() {
returnbirthdayNum;
}
/**
位时返回null
*@returnString
*/
public String getCheck_digitNum18() {
returncheck_digit;
}
publicint getLength() {
returnlength;
}
public String getSeqNum() {
returnseqNum;
}
}
import java.util.regex.*;
/**
*<p>Title:验证18/15位中国公民身份证号码,并分析有效的数据</p>
*<p>Description:</p>
</p>
*<p>Company:Labile</p>
*@authornotattributable
*@version1.0
*/
publicclass Verifier {
privatestaticfinal String verify18PatternStr = "(\\d{6})" + "(\\d{8})" + "(\\d{3})" + "([\\d[xX]]{1})";
privatestaticfinal Pattern verify18Pattern = Pattern.compile(verify18PatternStr);
privatestaticfinal String verify15PatternStr = "(\\d{6})" + "(\\d{6})" + "(\\d{3})";
privatestaticfinal Pattern verify15Pattern = Pattern.compile(verify15PatternStr);
/**
位的权
*/
privatestaticfinalintverify18Rights[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
/**
*检验码校对表
*/
privatestaticfinal String verify18CheckDigit[] = {"1","0","X","9","8","7","6","5","4","3","2"};
/**
*地区码
*/
private String areaNum;
/**
*出生时间
*/
private String birthdayNum;
/**
*顺序号
*/
private String seqNum;
/**
*校验码
*/
private String check_digit;
privateintlength;
publicboolean isMan() {
int num = Integer.parseInt(seqNum);
if(num%2==1) {
returntrue;
}
else
returnfalse;
}
/**
*得到生日的日期,格式:yyyy-mm-dd
*@returnString
*/
public String getBirthday() {
String FullBirthdayNum = null;
if(length==15)//在生日号码前加“19”
FullBirthdayNum = "19" + birthdayNum;
else
FullBirthdayNum = birthdayNum;
String year = FullBirthdayNum.substring(0, 4);
String month = FullBirthdayNum.substring(4, 6);
String date = FullBirthdayNum.substring(6, 8);
return year + "-" + month + "-" + date;
}
public Verifier(String IDCardNO) throws LengthIllegalException, CheckDigitInvalidException, NumberInvalidException {
if(IDCardNO == null)
thrownew NumberInvalidException();
length = IDCardNO.length();
switch(length) {
case 15:
Verifier15(IDCardNO);
break;
case 18:
Verifier18(IDCardNO);
break;
default:
thrownew LengthIllegalException();
}
}
privatevoid Verifier15(String IDCardNO) throws NumberInvalidException {
Matcher m = verify15Pattern.matcher(IDCardNO);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
areaNum = m.group(1);
birthdayNum = m.group(2);
seqNum = m.group(3);
}
privatevoid Verifier18(String IDCardNO) throws CheckDigitInvalidException, NumberInvalidException {
Matcher m = verify18Pattern.matcher(IDCardNO);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
areaNum = m.group(1);
birthdayNum = m.group(2);
seqNum = m.group(3);
check_digit = m.group(4);
//预期的校验位:
String expect_check_digit = getCheck_digit18(IDCardNO);
//如果校验位无效
if(expect_check_digit.equalsIgnoreCase(check_digit)==false)
thrownew CheckDigitInvalidException();
}
/**
位/17位身份证号算出校验位
*@paramIDCardNOString
*@returnString
*/
publicstatic String getCheck_digit18(String IDCardNO) {
//权总值
int sum = 0;
for(int i=0; i<=16; i++) {
int num = Integer.parseInt( IDCardNO.substring(i,i + 1) );
int right = verify18Rights[i];
sum = sum + num*right;
}
//对权总值取模
int y = sum % 11;
returnverify18CheckDigit[y];
}
publicstatic String IDCardNO15To18(String IDCardNO15) throws
NumberInvalidException {
Matcher m = verify15Pattern.matcher(IDCardNO15);
if(m.matches()==false) {
thrownew NumberInvalidException();
}
String NO17 = IDCardNO15.substring(0,6) + "19" + IDCardNO15.substring(6,15);
return NO17 + getCheck_digit18(NO17);
}
public String getAreaNum() {
returnareaNum;
}
public String getBirthdayNum() {
returnbirthdayNum;
}
/**
位时返回null
*@returnString
*/
public String getCheck_digitNum18() {
returncheck_digit;
}
publicint getLength() {
returnlength;
}
public String getSeqNum() {
returnseqNum;
}
}
- Java实现身份证号码校验
二话不说,直接上代码. package hope.identitycodecheck.demo; import java.text.DateFormat; import java.text.Simpl ...
- 国民身份证号码校验之“C#/Winform方法实现+案例分析”
根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出 ...
- JS011. 身份证号码校验(仅34行)
身份证格式 六位数字地址码 + 八位数字出生日期码 + 三位数字顺序码 + 一位数字校验码 checkIdCard.js checkIdCard: function (idCard){ //15位和1 ...
- JS身份证号码校验
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ]; // 加权因子 var ValideCode = [ 1, 0 ...
- js实现手机号码和身份证号码校验
<script type="text/javascript"> function checkform() { var re; var ss = document.get ...
- iOS开发之身份证号码校验
// // Card.h // THCStore // // Created by Mac on 15/7/15. // Copyright (c) 2015年 Mac. All rights ...
- 身份证号码的正则表达式及验证详解(JavaScript,Regex)
简言 在做用户实名验证时,常会用到身份证号码的正则表达式及校验方案.本文列举了两种验证方案,大家可以根据自己的项目实际情况,选择适合的方案. 身份证号码说明 居民身份证号码,正确.正式的称谓应该是&q ...
- 从身份证号码中获取性别、出生日期、籍贯,并更新mongodb
有这样的需求,人员信息是存在mongodb中,需要存放人员的身份证.性别.出生日期.籍贯等信息.通过脚本导入这些信息,但是只导入了身份证号码,其他信息空缺.现在需要补全其他信息. 其实身份证信息就包含 ...
- Python实现身份证号码有效性验证
# -*- coding: utf-8 -*- import re #Errors=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!' ...
随机推荐
- sqlplus登陆
cd \sqlplus sys@test_id as sysdba 切换用户SQL> connect system@test_id
- 使用UDEV绑定ASM多路径磁盘
OS版本:RHEL6.4 1) 找出目标磁盘的分区别名 # dmsetup ls |grep data data01 (253:9) data04 (253:4) data03 (253:3) dat ...
- CPU boot up过程
1. CPU0 BOOT CPU1 BOOT 通过IPC互相通信 2. CPU1 BOOT 完后,loop,等待IPC from CPU0 3. cpu0 写IPC通知CPU1,cpu1 ...
- linux 命令之 insmod
man insmod: INSMOD(8) insmod INSMOD(8) NAME insmod - Simple program to insert a module into the Linu ...
- C++Primer 第十四章
//1.当运算符作用于类类型运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令程序更加易于编写和阅读. //2.重载的运算符是具有特殊名字的函数,它们由关键字operato ...
- HUD 5086 Revenge of Segment Tree(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...
- 搞ACM的你们伤不起
这个虽然看过很多遍了,但是还是看着想笑,有时候真的想问问自己为什么这么菜,血流得还不够? 劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资 ...
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
这场CF又掉分了... 这题题意大概就给一个h*w的棋盘,中间有一些黑格子不能走,问只能向右或者向下走的情况下,从左上到右下有多少种方案. 开个sum数组,sum[i]表示走到第i个黑点但是不经过其他 ...
- CCF真题之日期计算
201509-2 日期计算 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有29天.满足下面条件之一的是闰年: 1) 年份是4的整数倍,而且不是100的整数倍: 2 ...
- Ajax 核心函数
今天刚刚了解了Ajax的一个核心函数,由于对javascript还没有系统的学习,目前还没有深入研究Ajax.但是在运用此函数的同时也发现了一些问题--编码问题.这样在后台接收到的URL参数若果有汉字 ...