Files
yuanhe-checkin-electron/src/utils/idCard.ts

15 lines
568 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function parseRegionCodesFromId(idNo: string | null | undefined) {
const result = { province: "", city: "", county: "" };
if (!idNo) return result;
const s = String(idNo).trim();
// 身份证号码前6位为行政区划代码
if (!/^[0-9]{6,}/.test(s)) return result;
const first6 = s.slice(0, 6);
const first4 = s.slice(0, 4);
const first2 = s.slice(0, 2);
result.county = first6; // 6位县级代码
result.city = `${first4}`; // 市级代码前4位+00
result.province = `${first2}`; // 省级代码前2位+0000
return result;
}