ID Card Verification

Source: Internet
Author: User

function Checkidcard (ID) {
var result=null;
var regExp =/^[a-za-z][0-9]{6}\ ([\w]{1}\) $/; Used to match Hong Kong identity card
if (id.length = = 15) {
Result=id.match (' \^ (11|12|31|51|15|65|54|64|45|81|82|23|22|21|13|14|63|37|41|32|34|35|36|43|42|44|46|62|61|51| 52|53|71|33|50) \\d{13}$ ');
if (Null==result) {
Return ' ID number is wrong! ';
}
return null;
} else if (id.length = = 18) {
Result=id.match ("\^ (11|12|31|51|15|65|54|64|45|81|82|23|22|21|13|14|63|37|41|32|34|35|36|43|42|44|46|62|61|51| 52|53|71|33|50) \\d{15}[xx0-9]$ ");
if (Null==result) {
Return ' ID number is wrong! ';
}

var check = 0;
var checknum = new Array (2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7);
var checkNum2 = new Array ("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
for (var i = 0; i <; i++) {
Check + = id.substring (16-i,17-i) * Checknum[i];
}
if (checknum2[(check%)]!=id.substring (17,18)) {
Return ' ID number is wrong! ';
}
return null;
}else if (regexp.test (ID)) {
return null
}
else {
Return ' ID number is wrong! ';
}
}

/**
* @ ID number Birthday Year
* 421022198810064521 = 1988
* 421022881006452 = 1988
*/
function Getbirthdayyearfromidcard (idcard) {
if (Idcard = = undefined) {
return null;
}
if (idcard.length = = 15) {
Return "+" + idcard.substring (6, 8);
}
if (idcard.length = = 18) {
Return idcard.substring (6, 10);
}
return null;
}

/**
* @ ID number Birthday Month
* 421022198810064521 = 10
* 421022881006452 = 10
*/
function Getbirthdaymonthfromidcard (idcard) {
if (Idcard = = undefined) {
return null;
}
if (idcard.length = = 15) {
Return Idcard.substring (8, 10);
}
if (idcard.length = = 18) {
Return Idcard.substring (10, 12);
}
return null;

}

/**
* @ ID number Birthday Month
* 421022198810064521 = 10
* 421022881006452 = 10
*/
function Getbirthdaydayfromidcard (idcard) {
if (Idcard = = undefined) {
return null;
}
if (idcard.length = = 15) {
Return Idcard.substring (10, 12);
}
if (idcard.length = = 18) {
Return Idcard.substring (12, 14);
}
return null;

}

/**
* @ ID number and birthday validation
*/
function Checkidcardandbirthday (Idcard, birthday) {
var idcardresult = Checkidcard (Idcard);
if (Idcardresult = = NULL && Birthday! = ") {
var yearfromidcard = Getbirthdayyearfromidcard (Idcard);
var monthfromidcard = Getbirthdaymonthfromidcard (Idcard);
var dayfromidcard = Getbirthdaydayfromidcard (Idcard);
var birthday_year = birthday.substring (0, 4);
var birthday_month = birthday.substring (5, 7);
var birthday_day = birthday.substring (8, 10);
if (yearfromidcard! = birthday_year) {
Return "The year of birth does not match the identity card. ";
}
if (monthfromidcard! = birthday_month) {
Return "The month of birth does not match the identity card. ";
}
if (dayfromidcard! = birthday_day) {
Return "date of birth is inconsistent with identity card. ";
}
}
return null;
}

ID Card Verification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.