Created
February 13, 2012 15:56
-
-
Save foxwoods/1817822 to your computer and use it in GitHub Desktop.
身份证号验证函数 | Validation function for Chinese I.D. card number
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
function validate_rid(id) { | |
// 18位身份证号 | |
// 国家标准《GB 11643-1999》 | |
function rid18(id) { | |
if(! /\d{17}[\dxX]/.test(id)) { | |
return false; | |
} | |
var modcmpl = function(m, i, n) { return (i + n - m % i) % i; }, | |
f = function(v, i) { return v * (Math.pow(2, i-1) % 11); }, | |
s = 0; | |
for(var i=0; i<17; i++) { | |
s += f(+id.charAt(i), 18-i); | |
} | |
var c0 = id.charAt(17), | |
c1 = modcmpl(s, 11, 1); | |
return c0-c1===0 || (c0.toLowerCase()==='x' && c1===10); | |
} | |
// 15位身份证号 | |
// 2013年1月1日起将停止使用 | |
// http://www.gov.cn/flfg/2011-10/29/content_1981408.htm | |
function rid15(id) { | |
var pattern = /[1-9]\d{5}(\d{2})(\d{2})(\d{2})\d{3}/, | |
matches, y, m, d, date; | |
matches = id.match(pattern); | |
y = +('19' + matches[1]); | |
m = +matches[2]; | |
d = +matches[3]; | |
date = new Date(y, m-1, d); | |
return (date.getFullYear()===y && date.getMonth()===m-1 && date.getDate()===d); | |
} | |
return rid18(id) || rid15(id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment