1010
1111module . 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 - z 0 - 9 ! # $ % & ' * + \/ = ? ^ _ ` { | } ~ . - ] + @ [ a - z 0 - 9 - ] + ( \. [ a - z 0 - 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 - z A - 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 [ 3 4 5 7 8 ] \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} ;
0 commit comments