身份证号检验java代码 java身份认证

java验证身份证号码是不是有效源代码

package com.yanlun.starter.utils;

创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站制作、光明网络推广、小程序定制开发、光明网络营销、光明企业策划、光明品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供光明建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

import java.util.regex.Pattern;

/**

* @author 作者:Yan,Email:yanlun0323@163.com

* @version 创建时间:2017年5月26日 上午10:42:09

*/

public class Validation {

public static final Pattern REX_DATE_YYYYMM_PATTERN = Pattern.compile("^[0-9]{6}$");

public static final Pattern REX_DATE_YYYYMMDD_PATTERN = Pattern.compile("^[0-9]{8}$");

// 身份证校验加权因子

public static final Integer[] ID_NUM_FACTOR = new Integer[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,

1 };

// 身份证第18位校验码

public static final String[] ID_NUM_PARITY_BIT = new String[] { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3",

"2" };

/*

 * 是否空字符串

 * 

 * @param c 文本或其它基本数字类型对象

 */

public static boolean isEmpty(Object c) {

return c == null || c.toString().trim().equals("");

}

/**

 * 判断是否为“”式的时期

 * 

 * @param dateStr

 * @return

 */

private static boolean isDate6(String dateStr) {

if (isEmpty(dateStr) || !REX_DATE_YYYYMM_PATTERN.matcher(dateStr).matches()) {

return false;

}

return isValidDateRange(date6Split(dateStr));

}

/**

 * 判断是否为“YYYYMMDD”式的时期

 * 

 * @param dateStr

 * @return

 */

private static boolean isDate8(String dateStr) {

if (isEmpty(dateStr) || !REX_DATE_YYYYMMDD_PATTERN.matcher(dateStr).matches()) {

return false;

}

return isValidDateRange(date8Split(dateStr));

}

private static boolean isLeapYear(Integer year) {

return ((year % 4 == 0)  (year % 100 != 0)) || (year % 400 == 0);

}

private static boolean isInvalidYear(Integer year) {

return year  1700 || year  2500;

}

private static boolean isInvalidMonth(Integer month) {

return month  1 || month  12;

}

private static boolean isInvalidDay(Integer day, Integer month, Integer year) {

Integer[] iaMonthDays = new Integer[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (isLeapYear(year))

iaMonthDays[1] = 29;

return day  1 || day  iaMonthDays[month - 1];

}

/**

 * split date 0-YY,1-MM,2-DD

 * 

 * @param dateStr

 * @return

 */

private static Integer[] date6Split(String dateStr) {

final Integer YEAR_BASE = 1900;

Integer year = null, month = null, day = null;

year = YEAR_BASE + Integer.valueOf(dateStr.substring(0, 2));

month = Integer.valueOf(dateStr.substring(2, 4));

day = Integer.valueOf(dateStr.substring(4, 6));

return new Integer[] { year, month, day };

}

/**

 * split date 0-YYYY,1-MM,2-DD

 * 

 * @param dateStr

 * @return

 */

private static Integer[] date8Split(String dateStr) {

Integer year = null, month = null, day = null;

year = Integer.valueOf(dateStr.substring(0, 4));

month = Integer.valueOf(dateStr.substring(4, 6));

if (dateStr.length() == 8) {

day = Integer.valueOf(dateStr.substring(6, 8));

return new Integer[] { year, month, day };

} else {

return new Integer[] { year, month };

}

}

private static boolean isValidDateRange(Integer[] dateSplitResult) {

Integer year = dateSplitResult[0], month = dateSplitResult[1], day = dateSplitResult[2];

if (isInvalidYear(year))

return false;

if (isInvalidMonth(month))

return false;

if (isInvalidDay(day, month, year))

return false;

return true;

}

/**

 * 18位/15位身份证号码校验

 * 

 * @param idNumber

 * @return

 */

public static boolean isIdentityCardNum(String idNumber) {

if (isEmpty(idNumber) || (idNumber.length() != 18  idNumber.length() != 15)) {

return false;

}

// initialize

if (idNumber.length() == 18) {

// check date

String date8 = idNumber.substring(6, 14);

if (isDate8(date8) == false) {

return false;

}

int totalMulAiWi = 0;

char charAt;

// check and set value, calculate the totalmulAiWi

for (int i = 0; i  17; i++) {

charAt = idNumber.charAt(i);

if (charAt  '0' || charAt  '9') {

return false;

}

totalMulAiWi += Integer.valueOf(String.valueOf(charAt)) * ID_NUM_FACTOR[i];

}

// calculate the check digit

String checkDigit = ID_NUM_PARITY_BIT[totalMulAiWi % 11];

// check last digit

if (!checkDigit.equalsIgnoreCase(String.valueOf(idNumber.charAt(17)))) {

return false;

}

} else {// length is 15

// check date

String date6 = idNumber.substring(6, 12);

if (isDate6(date6) == false) {

return false;

}

}

return true;

}

}

java用正则表达式判断一个18位身份证号是否有效

很显然 是你得正则表达式不对啊,正确的18位身份证验证正则为:

String regex = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

而且就算正则表达式正确了,你的逻辑判断代码也是有问题,

完成代码如下,请参考:

public class Homework {

public static void main(String[] args) {

String regex = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

System.out.println("请输入18位的身份证号码:");

Scanner scanner = new Scanner(System.in);

String s = scanner.nextLine();

if (s.matches(regex)) {

int y1, y2;

String year1 = s.substring(6, 8);

y1 = Integer.parseInt(year1);

String year2 = s.substring(8, 10);

y2 = Integer.parseInt(year2);

if ((y1 == 18  y2 = 87  y2 = 89)

|| (y1 == 19  y2 = 0  y2 = 99)

|| (y1 == 20  y2 = 17)) {

int m, d;

String month = s.substring(10, 12);

m = Integer.parseInt(month);

String day = s.substring(12, 14);

d = Integer.parseInt(day);

if ((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)

 (d == 31))

System.out.println("有效");

else if ((m == 4 || m == 6 || m == 9 || m == 11)  (d == 30))

System.out.println("有效");

else if (m == 2) {

int y;

String year = s.substring(6, 10);

y = Integer.parseInt(year);

if (((y % 4 == 0  y % 100 != 0) || y % 400 == 0)

 (d == 29))

System.out.println("有效");

else if (((y % 4 != 0  y % 100 == 0) || y % 400 != 0)

 (d == 28))

System.out.println("有效");

else

System.out.println("无效");

return;

} else {

System.out.println("无效");

return;

}

int ss;

String sex = s.substring(16, 17);

ss = Integer.parseInt(sex);

if (ss % 2 == 0)

System.out.println("女性");

else

System.out.println("男性");

} else

System.out.println("无效");

} else

System.out.println("无效");

}

}

用Java 写程序完成输入的身份证号码合法性判断

我国自1999年实施公民身份号码制度以来,许多公民身份号码末位为“X”的公民,由于不明白“X”的含义,要求给予更换,产生了不必要的误会。目前我国公民身份证号码由18位数字组成:前6位为地址码,第7至14位为出生日期码,第15至17位为顺序码,第18位为校验码。检验码分别是“0、1、2、……10”共11个数字,当检验码为“10”时,为了保证公民身份证号码18位,所以用“X”表示。虽然校验码为“X”不能更换,但若需全用数字表示,只需将18位公民身份号码转换成15位居民身份证号码,去掉第7至8位和最后1位3个数码。

当今的身份证号码有15位和18位之分。1985年我国实行居民身份证制度,当时签发的身份证号码是15位的,1999年签发的身份证由于年份的扩展(由两位变为四位)和末尾加了效验码,就成了18位。这两种身份证号码将在相当长的一段时期内共存。两种身份证号码的含义如下:

18位的身份证号码 如:130429####%%%%0078

1~6位为地区代码,其中1、2位数为各省级政府的代码,3、4位数为地、市级政府的代码,5、6位数为县、区级政府代码。如13(河北省)04(邯郸市)29(永年县)

7~14位为出生年月日

15~17位为顺序号,是县、区级政府所辖派出所的分配码,每个派出所分配码为10个连续号码,例如“000-009”或“060-069”,其中单数为男性分配码,双数为女性分配码,如遇同年同月同日有两人以上时顺延第二、第三、第四、第五个分配码。如:007的就是个男生 而且和他同年月日生的男生至少有两个 他们的后四位是001* 和 003*

18位为效验位(识别码),通过复杂公式算出,普遍采用计算机自动生成。是前面17位的一种检验代码,如果你改变了前面某个数字而后面的效验代码不响应改变就会被计算软件判断为非法身份正号码。X也是效验代码的一中

15位的身份证号码:

(1)1~6位为地区代码

(2)7~8位为出生年份(2位),9~10位为出生月份,11~12位为出生日期

(3)第13~15位为顺序号,并能够判断性别,奇数为男,偶数为女。

使用java判断输入身份证号位数是否正确,判断输入是否有效

使用java语言实现一个身份证号码转换的程序:把15位号码转换为18位的号码功能要求:1.判断输入身份证号位数是否正确,判断输入是否有效(如输入非数字字符),否则提示用户重新输入。

2.计算身份证号码。

3.根据身份证号的第7-14位(YYYYMMDD)显示出生日期。4.根据身份证号的第17位(奇数为男性,偶数为女性),显示性别。

用JAVA语言写,下面是二代身份证号码的检查函数的头部,请根据文档注释完成函数体。

public class CheckID {

/**

* 二代身份证号码检查函数

* @param id 二代身份证号码

* @return 0合格 -1号码是空号 -2号码位数不对 -3含有非法号码 -4日期(年月日)不对 -5校验码不对

*/

public static int idCheck(String id){

if(id.equals(""))

return -1;

if(id.length() != 18)

return -2;

if(!checkNum(id))

return -3;

if(!checkDate(id))

return -4;

if(!checkCode(id))

return -5;

return 0;

}

//检查每一位是否正确

public static boolean checkNum(String id){

for(int i = 0; iid.length() -1; i++){

if(!Character.isDigit(id.charAt(i)))

return false;

}

//判断最后一位,如果不是数字而且也不为X

if(!(Character.isDigit(id.charAt(17)) || id.charAt(17)=='X'))

return false;

return true;

}

//检查日期格式

public static boolean checkDate(String id){

int year = Integer.parseInt(id.substring(6, 10));

int month = Integer.parseInt(id.substring(10, 12));

int day = Integer.parseInt(id.substring(12, 14));

if(year2000 || year1880)

return false;

if(month12 || month1)

return false;

//处理每一个月份所对应的天数是否合法

if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){

if(day31 || day1)

return false;

}else if(month==4 || month==6 || month==9 || month==11){

if(day30 || day1)

return false;

}else{

if((year%4==0 year%100!=0) || year%400==0){

if(day29 || day1)

return false;

}else{

if(day28 || day1)

return false;

}

}

return true;

}

//检查校验码

public static boolean checkCode(String id){

int[] codes = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

char[] result = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

int sum = 0;

for(int i = 0; iid.length()-1; i++){

sum += (Integer.parseInt(id.substring(i, i+1))) * codes[i];

}

if(result[sum%11] != id.charAt(17))

return false;

return true;

}

java代码怎么校验身份证号码含有非法字符

如果只要判断有非法的字符(除0-9和Xx外)可用正则表达式publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereStrings="2142213weqrwe32";StringregEx="[^0-9Xx]";Patternpat=Pattern.compile(regEx);Matchermat=pat.matcher(s);booleanrs=mat.find();if(rs){System.out.print("有非法字符");}另外,校验身份证号码有专门程序的,可直接校验身份证号是否正确,在自己在网上找下


网站栏目:身份证号检验java代码 java身份认证
链接地址:http://lszwz.com/article/hgphdd.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款