转载地址: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=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!' ...
随机推荐
- Python生成8位随机密码
#!/usr/bin/env python # -*- coding: utf- -*- import random import string #第一种方法 seed = "1234567 ...
- c# 排序算法总结
/// <summary> /// 冒泡排序法1 /// </summary> /// <param name="list"></para ...
- linux: centos设置ip以及连接外网
注明:我使用的的使centos 7,所有文件名是ifcfg-enp0s3, 一. 设置虚拟机中linux的ip,使本地能连通虚拟机的linux系统 1>.进入本地windows的cmd,输入ip ...
- java中的类修饰符、成员变量修饰符、方法修饰符。
类修饰符: public(访问控制符),将一个类声明为公共类,他可以被任何对象访问,一个程序的主类必须是公共类. abstract,将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. fin ...
- object_id的用法
OBJECT_ID: 返回数据库对象标识号. 语法 OBJECT_ID ( 'object' ) 参数 'object' 要使用的对象.object 的数据类型为 char 或 nchar.如果 ob ...
- iOS缓存使用的框架
MagicalRecord FMDB 都可以在gitHub上找到
- codevs 1506 传话
http://codevs.cn/problem/1506/ 1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 D ...
- nyist 606 ACM之路
http://acm.nyist.net/JudgeOnline/problem.php?pid=606 ACM之路 时间限制:1000 ms | 内存限制:65535 KB 描述 转眼间,12级新生 ...
- [转]gitHub客户端Desktop的安装使用总结 ---基础篇
gitHub客户端Desktop的安装使用总结 ---基础篇 发表于2015/12/11 11:41:57 8399人阅读 分类: Android之应用实战 这段时间想把我写的东西上传到github ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...