-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { isChineseIDCardNumber } from './isChineseIDCardNumber' | ||
|
||
describe(isChineseIDCardNumber.name, () => { | ||
test('不是合法的身份证号', () => { | ||
for (const value of [ | ||
'2000', | ||
'190101881101231', | ||
'110101881301231', | ||
'110101198811214398', | ||
'11010119881101331a', | ||
'469001399208187005', | ||
'46900119925818180x', | ||
'530627199508918277', | ||
'110106100001019457', // 1000 年 | ||
'140425900001017773', // 9000 年 | ||
]) { | ||
expect(isChineseIDCardNumber(value)).toBeFalse() | ||
} | ||
}) | ||
|
||
test('是合法的身份证号', () => { | ||
for (const value of [ | ||
'110101198811014398', | ||
'11010119881101331X', | ||
'469001199208187005', | ||
'46900119920818180x', | ||
]) { | ||
expect(isChineseIDCardNumber(value)).toBeTrue() | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const testRegExp = /^[1-9][0-9]{16}[0-9Xx]$/ | ||
// prettier-ignore | ||
const areaMap = [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] | ||
const weightMap = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] | ||
const codeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] | ||
|
||
const isValidDate = (year: number, month: number, day: number): boolean => { | ||
const date = new Date(year, month - 1, day) | ||
return ( | ||
date.getFullYear() === year && | ||
date.getMonth() + 1 === month && | ||
date.getDate() === day && | ||
date.getTime() < new Date().getTime() && | ||
year > 1900 | ||
) | ||
} | ||
|
||
/** | ||
* 检测传入的值是否是合法的中国大陆居民 `18` 位身份证号码。 | ||
* | ||
* ``` | ||
* isChineseIDCardNumber('123456') // => false | ||
* ``` | ||
* | ||
* @param value 要检测的值 | ||
* @returns 返回检测结果 | ||
*/ | ||
export function isChineseIDCardNumber(value: string): boolean { | ||
const len = value.length | ||
|
||
// 长度错误 | ||
if (len !== 18) { | ||
return false | ||
} | ||
|
||
// 模式校验 | ||
if (!testRegExp.test(value)) { | ||
return false | ||
} | ||
|
||
// 地区校验 | ||
if (areaMap.indexOf(+value.substr(0, 2)) === -1) { | ||
return false | ||
} | ||
|
||
// 出生日期 | ||
if ( | ||
!isValidDate( | ||
+value.substr(6, 4), | ||
+value.substr(10, 2), | ||
+value.substr(12, 2), | ||
) | ||
) { | ||
return false | ||
} | ||
|
||
// 校验码 | ||
const sum = value | ||
.split('') | ||
.slice(0, 17) | ||
.reduce((s, num, index) => { | ||
s += +num * weightMap[index] | ||
return s | ||
}, 0) | ||
const code = codeMap[sum % 11] | ||
return code === value[17].toUpperCase() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters