JavaScript ID number Validation instance code

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> website special effects ID number validation instance code </title>
<script>
function Isidcardno (num)
{
var Factorarr = new Array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var error;
var vararray = new Array ();
var intvalue;
var lngproduct = 0;
var intcheckdigit;
var intstrlen = num.length;
var idnumber = num;
Initialize
if ((Intstrlen!=) && (Intstrlen!= 18))
{
return false;
}
Check and set value
for (i=0;i<intstrlen;i++)
{
Vararray[i] = Idnumber.charat (i);
if ((Vararray[i] < ' 0 ' | | | vararray[i] > ' 9 ') && (i!= 17))
{
return false;
}
else if (I < 17)
{
Vararray[i] = Vararray[i]*factorarr[i];
}
}

if (Intstrlen = 18)
{
Check Date
var Date8 = idnumber.substring (6,14);
if (checkdate (date8) = = False)
{
Error = "The date information in the ID card is incorrect!.";
return false;
}
Calculate the sum of the products
for (i=0;i<17;i++)
{
Lngproduct = lngproduct + vararray[i];
}
Calculate the check digit
Intcheckdigit = 12-lngproduct% 11;
Switch (intcheckdigit)
{
Case 10:
Intcheckdigit = ' x ';
Break
Case 11:
Intcheckdigit = 0;
Break
Case 12:
Intcheckdigit = 1;
Break
}
Check last digit
if (Vararray[17].touppercase ()!= intcheckdigit)
{
return false;
}
}
Else
{//length is 15
Check Date
var date6 = idnumber.substring (6,12);
if (checkdate (date6) = = False)
{
Alert ("ID date information is incorrect!.");
return false;
}
}
return true;
}

function Checkdate (date)
{
return true;
}

function test ()
{
var id = document.getElementById (' id '). value;
alert (ID);
Alert (Isidcardno (ID));
}
</script>

<body>
<tr>
&LT;TD align= "Right" class= "Reg-item" ><span class= "color" >*</span> ID card:</td>
<td><input id= "User_sfz" class= "Input_out" name= "USER_SFZ" type= "text" onfocus= "onfuos (' User_sfz ')" onblur= "Regsfz ()" onmousemove= "This.classname= ' input_move '" Onmouseo tutorial ut= "this.classname= ' input_out '" style= "width:250px ; "/></td>
<td><span id= "SFZ" ></span></td>
</tr>
</body>
Rules of the ID card number "
1, 15-digit ID card number composition:
Ddddddyymmddxxs a total of 15 places, of which:
The DDDDDD is a 6-bit local code, based on which the 6-bit can obtain the location of the ID number.
YY is a 2-bit year code, is the identity card holder of the year of origin.
MM is a 2-bit month code, is the identity card holder's birth month.
DD is a 2-digit date code, which is the birth date of the identity card holder.
These 6 together form the date of birth of the identity card holder.
XX is a 2-bit sequential code, this is a random number.
S is a 1-digit sex code, with odd numbers representing males and even women.

2, 18-digit ID card number composition:
DDDDDDYYYYMMDDXXSP a total of 18 places, of which:
The other parts are the same as the 15-bit. The year code is upgraded from the original 2 to the 4-bit. The last one is a check digit.
The validation rules are:
(1) The weighted summation formula of 17-digit body code
s = SUM (AI * wi), i = 0, ..., 16, first the sum of the first 17 digits
AI: Indicates the ID number value of position I
WI: A weighted factor representing position I
Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4-2
(2) Computational model
y = mod (s, 11)
(3) The corresponding check code is obtained through the model
Y:0 1 2 3 4 5 6 7 8 9 10
Check code: 1 0 x 9 8 7 6 5 4 3 2

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.