Skip to content

Commit dd99e93

Browse files
committed
v0.0.3
1. Add isChineseIdCard method 2. Update README
1 parent aa1cb95 commit dd99e93

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ node-validator
33

44
String validator for more.
55

6+
[![NPM version][npm-image]][npm-url]
7+
[![build status][travis-image]][travis-url]
8+
[![David deps][david-image]][david-url]
9+
10+
[npm-image]: https://img.shields.io/npm/v/is-valid.svg?style=flat
11+
[npm-url]: https://npmjs.org/package/is-valid
12+
[travis-image]: https://img.shields.io/travis/SFantasy/node-validator.svg?style=flat
13+
[travis-url]: https://travis-ci.org/SFantasy/node-validator
14+
[david-image]: https://img.shields.io/david/SFantasy/node-validator.svg?style=flat
15+
[david-url]: https://david-dm.org/SFantasy/node-validator
16+
617
## Install
718

819
```
@@ -24,6 +35,7 @@ validator.isEmail('abc@gmail.com'); // => true
2435
- isAllEnglish(str): check if it is a string only contains English characters
2536
- isAllDigit(str): check if it is a string only contains digits
2637
- isChineseTel(str): check if it is a Chinese cell-phone number
38+
- isChineseIdCard(str): check if it is a 18-digit Chinese ID card number
2739

2840
## License
2941

bin/is-valid

Lines changed: 0 additions & 2 deletions
This file was deleted.

example/example.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ console.log(validator.isAllEnglish('test'));
2121

2222
// isAllDigit
2323
console.log(validator.isAllDigit('12345'));
24+
25+
// isChineseIdCard
26+
console.log(validator.isChineseIdCard('330682199110273013'));

lib/index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,91 @@
1010

1111
module.exports = {
1212

13+
/**
14+
* check if it is an Email string
15+
* @param str
16+
* @returns {boolean}
17+
*/
1318
isEmail: function (str) {
1419
return /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*$/i.test(str);
1520
},
1621

22+
/**
23+
* check if it is a string only contains Chinese characters
24+
* @param str
25+
* @returns {boolean}
26+
*/
1727
isAllChinese: function (str) {
1828
return !/[^\u4e00-\u9fa5]/.test(str);
1929
},
2030

31+
/**
32+
* check if it is a string only contains English characters
33+
* @param str
34+
* @returns {boolean}
35+
*/
2136
isAllEnglish: function (str) {
2237
return /^[a-zA-Z]+$/.test(str);
2338
},
2439

40+
/**
41+
* check if it is a string only contains digits
42+
* @param str
43+
* @returns {boolean}
44+
*/
2545
isAllDigit: function (str) {
2646
return /^[0-9]+$/.test(str);
2747
},
2848

49+
/**
50+
* check if it is a Chinese cell-phone number
51+
* @param str
52+
* @returns {boolean}
53+
*/
2954
isChineseTel: function (str) {
3055
return /^1[34578]\d{9}$/.test(str);
56+
},
57+
58+
/**
59+
* check if it is a 18-digit Chinese ID card number
60+
* @param str
61+
* @returns {boolean}
62+
*/
63+
isChineseIdCard: function (str) {
64+
65+
if (!(str.length === 18)) {
66+
return false;
67+
} else {
68+
return isBirthValid(str) && isCodeValid(str);
69+
}
70+
71+
function isBirthValid (num) {
72+
var year = num.substring(6, 10);
73+
var month = num.substring(10, 12);
74+
var day = num.substring(12, 14);
75+
var temp = new Date(year, parseInt(month, 10), parseInt(day, 10));
76+
77+
return (temp.getFullYear() === parseInt(year, 10) &&
78+
temp.getMonth() === parseInt(month, 10) &&
79+
temp.getDate() === parseInt(day, 10));
80+
}
81+
82+
function isCodeValid (num) {
83+
var wc = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
84+
var validCodeArr = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
85+
var sum = 0;
86+
var temp = num.split('');
87+
88+
if (temp[17].toLowerCase() === 'x') {
89+
temp[17] = 10;
90+
}
91+
92+
for (var i = 0; i < temp.length; i++) {
93+
sum += wc[i] * temp[i];
94+
}
95+
96+
return temp[17] === validCodeArr[sum % 11];
97+
}
3198
}
3299

33100
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "is-valid",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "String validator for more",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)