Verify ID card validity using Javascript

Source: Internet
Author: User

Please refer to the instance code for the tutorial of helper House (www.bkjia.com:

Reference content is as follows:
<Script language = "javascript">
Function isChinaIDCard (){
StrNo = document. getElementById ("<% = txtCarID. ClientID %>"). value;
If (StrNo. length = 15 ){
If (! IsValidDate ("19" + StrNo. substr (6, 2), StrNo. substr (8, 2), StrNo. substr (10, 2) {return false ;}
} Else if (StrNo. length = 18 ){
If (! IsValidDate (StrNo. substr (6, 4), StrNo. substr (10, 2), StrNo. substr (12, 2) {return false ;}
} Else {
ShowLayer ('popupbox', "the ID card number must be 15 or 18 characters! ");
Return false;
}

If (StrNo. length = 18)
{
Var a, B, c
If (! IsNumber (StrNo. substr () {showLayer ('popupbox', "the ID number is incorrect. The first 17 digits cannot contain English letters! "); Return false ;}
A = parseInt (StrNo. substr () * 7 + parseInt (StrNo. substr () * 9 + parseInt (StrNo. substr () * 10;
A = a + parseInt (StrNo. substr () * 5 + parseInt (StrNo. substr () * 8 + parseInt (StrNo. substr () * 4;
A = a + parseInt (StrNo. substr (6, 1) * 2 + parseInt (StrNo. substr (7, 1) * 1 + parseInt (StrNo. substr (8, 1) * 6;
A = a + parseInt (StrNo. substr () * 3 + parseInt (StrNo. substr () * 7 + parseInt (StrNo. substr () * 9;
A = a + parseInt (StrNo. substr () * 10 + parseInt (StrNo. substr () * 5 + parseInt (StrNo. substr () * 8;
A = a + parseInt (StrNo. substr (15,1) * 4 + parseInt (StrNo. substr (16,1) * 2;
B = a % 11;
If (B = 2) // the last digit is the check bit.
{
C = StrNo. substr (17,1). toUpperCase (); // convert to uppercase X
}
Else
{
C = parseInt (StrNo. substr (17,1 ));
}
Switch (B)
{
Case 0: if (c! = 1) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be: 1"); return false;} break;
Case 1: if (c! = 0) {showLayer ('popupbox', "Incorrect ID card number check bit: the last digit should be: 0"); return false;} break;
Case 2: if (c! = "X") {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be: X"); return false;} break;
Case 3: if (c! = 9) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be 9"); return false;} break;
Case 4: if (c! = 8) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be 8"); return false;} break;
Case 5: if (c! = 7) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be 7"); return false;} break;
Case 6: if (c! = 6) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be 6"); return false;} break;
Case 7: if (c! = 5) {showLayer ('popupbox', "Incorrect ID card number check bit: the last digit should be: 5"); return false;} break;
Case 8: if (c! = 4) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be 4"); return false;} break;
Case 9: if (c! = 3) {showLayer ('popupbox', "Incorrect ID card number check bit: the last digit should be: 3"); return false;} break;
Case 10: if (c! = 2) {showLayer ('popupbox', "check digit of the ID card is incorrect: the last digit should be: 2"); return false ;}
}
} Else {// 15-digit ID card number
If (! IsNumber (StrNo) {showLayer ('popupbox', "the ID card number is incorrect. The first 15 digits cannot contain English letters! "); Return false ;}
}
Return true;

}

Function isValidDate (iY, iM, iD ){
If (iY> 2009 | iY <1900 |! IsNumber (iY )){
ShowLayer ('popupbox', "Enter ID card number, year" + iY + "illegal! ");
Return false;
}
If (iM> 12 | iM <= 0 |! IsNumber (iM )){
ShowLayer ('popupbox', "Enter ID card number, Month" + iM + "illegal! ");
Return false;
}
If (iD> 31 | iD <= 0 |! IsNumber (iD )){
ShowLayer ('popupbox', "Enter iD card number, date" + iD + "illegal! ");
Return false;
}
Return true;
}
/**
* Verify if it is a number.
*/
Function isNumber (oNum ){
If (! ONum) return false;
Var strP =/^ \ d + (\. \ d + )? $ /;
If (! StrP. test (oNum) return false;
Try {
If (parseFloat (oNum )! = ONum) return false;
}
Catch (ex)
{
Return false;
}
Return true;
}
Function showLayer (id, txt ){
Document.getelementbyid(id).style.height?document.doc umentElement. scrollHeight;
Document. getElementById (id). style. display = "block ";
Document. getElementById ("txtid"). innerText = txt;
Document. body. style. overflow = "hidden ";
Document. body. scrollTop = 0;
}

Function hideLayer (id ){
Document. getElementById (id). style. display = "none ";
Document. body. style. overflow = "visible ";
}

</Script>

<! -- Pop-up layer: start -->
<Div class = "popupBox" id = "popupBox">
<Iframe class = "popupFrame"> </iframe>
<Div class = "popupMask"> </div>
<Div class = "popupContent" id = "popupContent">
<H3 id = "txtid"> <Button onclick = "hideLayer ('popupbox')"> </button>
</Div>
</Div>
<! -- Pop-up layer: end -->

Related Article

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.