npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

idcard-parse-utils

v1.0.3

Published

身份证号校验

Readme

idcard-parse-utils

一个用于校验中国身份证号码的 JavaScript 库,支持 18 位和 15 位身份证号码的校验。

特性

  • ✅ 支持 18 位和 15 位身份证号码校验
  • ✅ 校验省份编码的合法性
  • ✅ 校验出生日期的合理性
  • ✅ 校验 18 位身份证号码的校验位
  • ✅ 支持 CommonJS 和 ES Module 两种模块格式
  • ✅ 支持 TypeScript 类型定义
  • ✅ 无依赖,体积小巧

安装

npm install idcard-parse-utils

或者使用 yarn:

yarn add idcard-parse-utils

或者使用 pnpm:

pnpm add idcard-parse-utils

使用方法

ES Module

import { checkPsidno } from "idcard-parse-utils";

// 校验18位身份证号
const result1 = checkPsidno("110101199001011234");
console.log(result1); // true 或 false

// 校验15位身份证号
const result2 = checkPsidno("110101900101123");
console.log(result2); // true 或 false

TypeScript

import { checkPsidno } from "idcard-parse-utils";

// 类型安全的身份证号校验
const result1: boolean = checkPsidno("110101199001011234");
const result2: boolean = checkPsidno(110101199001011234);

// TypeScript 会提供完整的类型检查和智能提示
console.log(result1); // boolean
console.log(result2); // boolean

CommonJS

const { checkPsidno } = require("idcard-parse-utils");

// 校验身份证号
const result = checkPsidno("110101199001011234");
console.log(result); // true 或 false

在浏览器中使用

<script src="https://unpkg.com/idcard-parse-utils/dist/index.umd.js"></script>
<script>
  const result = idCardParse.checkPsidno("110101199001011234");
  console.log(result);
</script>

API 文档

checkPsidno(value)

校验身份证号码是否合规。

参数:

  • value (String|Number): 需要校验的身份证号码

返回值:

  • Boolean: 返回 true 表示身份证号码合规,返回 false 表示不合规

示例:

import { checkPsidno } from "idcard-parse-utils";

// 有效的身份证号码
console.log(checkPsidno("110101199001011234")); // 根据实际校验结果

// 无效的身份证号码
console.log(checkPsidno("123456789012345678")); // false
console.log(checkPsidno("11010119900101123")); // false (格式错误)
console.log(checkPsidno("991101199001011234")); // false (省份编码错误)

校验规则

该库会对身份证号码进行以下校验:

1. 格式校验

  • 18 位身份证号码:前 17 位为数字,第 18 位为数字或 X
  • 15 位身份证号码:全部为数字

2. 省份编码校验

支持的省份编码包括:

  • 11: 北京
  • 12: 天津
  • 13: 河北
  • 14: 山西
  • 15: 内蒙古
  • 21: 辽宁
  • 22: 吉林
  • 23: 黑龙江
  • 31: 上海
  • 32: 江苏
  • 33: 浙江
  • 34: 安徽
  • 35: 福建
  • 36: 江西
  • 37: 山东
  • 41: 河南
  • 42: 湖北
  • 43: 湖南
  • 44: 广东
  • 45: 广西
  • 46: 海南
  • 50: 重庆
  • 51: 四川
  • 52: 贵州
  • 53: 云南
  • 54: 西藏
  • 61: 陕西
  • 62: 甘肃
  • 63: 青海
  • 64: 宁夏
  • 65: 新疆
  • 71: 台湾
  • 81: 香港
  • 82: 澳门
  • 91: 国外

3. 出生日期校验

  • 出生日期不能是未来时间
  • 年龄不能超过 150 岁
  • 月份必须在 1-12 之间
  • 日期必须符合该月的实际天数

4. 校验位校验(仅 18 位)

对 18 位身份证号码,会根据前 17 位计算校验位,并与第 18 位进行比对。

使用示例

import { checkPsidno } from "idcard-parse-utils";

// 用户注册时校验身份证号
function validateUser(userInfo) {
  const { idCard } = userInfo;

  if (!checkPsidno(idCard)) {
    throw new Error("身份证号码格式不正确");
  }

  // 继续其他验证逻辑...
}

// 表单验证
function validateForm() {
  const idCardInput = document.getElementById("idCard");
  const idCard = idCardInput.value.trim();

  if (!checkPsidno(idCard)) {
    alert("请输入正确的身份证号码");
    return false;
  }

  return true;
}

// 批量校验
const idCards = ["110101199001011234", "110101900101123", "123456789012345678"];

idCards.forEach((idCard) => {
  console.log(`${idCard}: ${checkPsidno(idCard) ? "有效" : "无效"}`);
});

构建和开发

# 安装依赖
pnpm install

# 构建项目
pnpm build

# 清理构建文件
pnpm clean

v1.0.0

  • 初始版本
  • 支持 18 位和 15 位身份证号码校验
  • 支持省份编码校验
  • 支持出生日期合理性校验
  • 支持 18 位身份证校验位校验

声明

  • 抄的