转载地址: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=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!' ...
随机推荐
- 建立自己的Yum源
转自http://kicklinux.com/setup-yum-repos-server/ 命令 reposync 可以直接同步yum源 如/etc/yum.repos.d/cloudera-cdh ...
- python pickle 和 shelve模块
pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...
- MySQL的半同步复制监控
(1)master端 >show variables like 'rpl_semi_sync%'; +------------------------------------+-------+ ...
- mini install centOS log
no ifconfig yum install net-tools ------------------------------------------------------------------ ...
- volatile 用法
volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错,(VC++ 在产生release版可执行码时会进 ...
- Leetcode: Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- git的基本使用
1.在本地新建一个文件夹来存放代码 2.用命令行进入这个文件夹 3.git init --来创建一个代码仓库 3. 配置用户信息:用户名和 邮箱(联系作者本人沟通, 责任到人) git config ...
- [转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“
mongodb 查询条件 这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte" ...
- gson使用注意事项
public static Object toBean(String jsonString, Class<?> beanclass) { GsonBuilder gsonb = new G ...
- Node.js Express 获取request原始数据
app.use(bodyParser.json());客户端请求接口时如果指名请求头类型 为Content-Type=application/jsonbodyParser 会自动将 body 里的 j ...