Giter Club home page Giter Club logo

chinese-idcard-checker's Introduction

chinese-idcard-checker

npm Build Status Coverage Status

🇨🇳中华人民共和国居民身份证验证器

安装

yarn add chinese-idcard-checker

如果使用npm则执行 npm install chinese-idcard-checker --save 安装

用法

import {
  validate,
  province,
  birthDate,
  gender,
  pattern,
} from 'chinese-idcard-checker';
// or 
// const IDCardChecker = require('chinese-idcard-checker'); 

// 验证身份证有效性
validate(idCardNum); // true or false

// 获取省份
province(idCardNum); // '北京市'

// 获取出生日期
birthDate(idCardNum);  // new Date('1949-10-1')

// 获取性别
gender(idCardNum); // '男','女'

// 返回正则表达式常量
if(pattern.test(idCardNum)){
  // ...
}

API

pattern ⇒ RegExp

返回正则表达式

Returns: RegExp - /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

validate(idCardNum) ⇒ boolean

验证身份证有效性

Param Type Description
idCardNum string 身份证号码

province(idCardNum) ⇒ string | null

获取身份证所在省份

Returns: string | 'null' - 省份

Param Type Description
idCardNum string 身份证号码

birthDate(idCardNum) ⇒ Date | null

获取出生日期

Returns: Date | null - 出生日期

Param Type Description
idCardNum string 身份证号码

gender(idCardNum) ⇒ string | null

获取性别

Returns: string | null - '男','女'

Param Type Description
idCardNum string 身份证号码

LICENSE

MIT License

chinese-idcard-checker's People

Contributors

dependabot[bot] avatar weihongyu12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chinese-idcard-checker's Issues

15位身份证号码处理

/**
* 身份证15位18位互转
* @param {string} oldCard - 身份证号码
* @return 18或15位身份证号码
*/
IDCardChecker.prototype.getIDCard = function (oldCard) {
try {
if (oldCard.length == 18) {
var strZJHM = oldCard.substr(0, 6) + oldCard.substr(8, 9);
return strZJHM.toLocaleUpperCase();
}
else if (oldCard.length == 15) {
var w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var a = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
var strZJHM = oldCard.substr(0, 6) + "19" + oldCard.substr(6);
var v_Sum = 0;
for (var i = 0; i < strZJHM.length; i++) {
v_Sum = v_Sum + Number(strZJHM.substr(i, 1)) * w[i];
}
var ai = v_Sum % 11;
strZJHM = strZJHM + a[ai];
return strZJHM.toLocaleUpperCase();
}
else {
throw new Error('身份证格式错误!');
}
}
catch (e) {
throw new Error('身份证格式错误!');
}
};

safari 下获取生日失败

原因在于这段

getBirthDate(idCardNum: string): Date | null {
if (this.validate(idCardNum)) {
const birthDate = idCardNum.substring(6, 14);
const year = this.getYear(birthDate);
const month = this.getMonth(birthDate);
const date = this.getDay(birthDate);
return new Date(`${year}-${month}-${date}`);
}
return null;
}

const date = this.getDay(dateString);
const fullDate = new Date(`${year}-${month}-${date}`);
if (fullDate && fullDate.getMonth() === month - 1) {
return true;

return new Date(`${year}-${month}-${date}`);

如果生日是单为数

比如 2000-1-1,在 safari 下 new Date('2000-1-1') 会抛出异常

把 0 补齐就没问题 new Date('2000-01-01')

截屏2020-08-18 15 47 21

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.