IDValidate. js
/**
* ID card 15-bit encoding rule: dddddd yymmdd xx p
* Dddddd: Location Code
* Yymmdd: Date of birth
* Xx: sequence class encoding, which cannot be determined
* P: gender. The odd number is male and the even number is female.
* <P/>
* ID card 18-bit encoding rule: dddddd yyyymmdd xxx y
* Dddddd: Location Code
* Yyyymmdd: Date of birth
* Xxx: sequence class code, which cannot be determined. The odd number is male and the even number is female.
* Y: Check Code. The value can be obtained through the first 17 digits.
* <P/>
* The 18-digit number weighting factor is (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]
* Verification digit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]
* Formula for calculating the checkpoint: Y_P = mod (Σ (Ai × Wi), 11)
* I is the 2 .. 18 bits of the ID card number from right to left; Y_P is the position of the checkcode array where the ankle checkcode is located
*
*/
Var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // Weighting Factor
Var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // ID card verification bid value. 10 represents X
Function IdCardValidate (idCard ){
IdCard = trim (idCard. replace (// g ,""));
If (idCard. length = 15 ){
Return isValidityBrithBy15IdCard (idCard );
} Else if (idCard. length = 18 ){
Var a_idCard = idCard. split (""); // obtain the ID card Array
If (isValidityBrithBy18IdCard (idCard)
& IsTrueValidateCodeBy18IdCard (a_idCard )){
Return true;
} Else {
Return false;
}
} Else {
Return false;
}
}
/**
* Check whether the last verification digit is correct when the ID card number is 18
* @ Param a_idCard ID card number Array
* @ Return
*/
Function isTrueValidateCodeBy18IdCard (a_idCard ){
Var sum = 0; // declare the weighted sum variable
If (a_idCard [17]. toLowerCase () = 'X '){
A_idCard [17] = 10; // Replace the last x verification code with 10 for subsequent operations
}
For (var I = 0; I <17; I ++ ){
Sum + = Wi [I] * a_idCard [I]; // weighted sum
}
ValCodePosition = sum % 11; // the location where the verification code is obtained
If (a_idCard [17] = ValideCode [valCodePosition]) {
Return true;
} Else {
Return false;
}
}
/**
* Identify a male or female by ID card
* @ Param idCard: 15/18-digit ID card number
* @ Return 'female '-female, 'male'-male
*/
Function maleOrFemalByIdCard (idCard ){
IdCard = trim (idCard. replace (// g, ""); // process the ID card number. Contains spaces between characters.
If (idCard. length = 15 ){
If (idCard. substring (14,15) % 2 = 0 ){
Return 'female ';
} Else {
Return 'male ';
}
} Else if (idCard. length = 18 ){
If (idCard. substring (14,17) % 2 = 0 ){
Return 'female ';
} Else {
Return 'male ';
}
} Else {
Return null;
}
// The input characters can be processed as arrays.
// If (idCard. length = 15 ){
// Alert (idCard [13]);
// If (idCard [13] % 2 = 0 ){
// Return 'female ';
//} Else {
// Return 'male ';
//}
//} Else if (idCard. length = 18 ){
// Alert (idCard [16]);
// If (idCard [16] % 2 = 0 ){
// Return 'female ';
//} Else {
// Return 'male ';
//}
//} Else {
// Return null;
//}
}
/**
* Verify whether the birthday in the 18-digit ID card number is a valid birthday.
* @ Param idCard 18-digit book id card string
* @ Return
*/
Function isValidityBrithBy18IdCard (idCard18 ){
Var year = idCard18.substring (6, 10 );
Var month = idCard18.substring (10, 12 );
Var day = idCard18.substring (12, 14 );
Var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day ));
// GetFullYear () is used to obtain the year to avoid the millennium bug.
If (temp_date.getFullYear ()! = ParseFloat (year)
| Temp_date.getMonth ()! = ParseFloat (month)-1
| Temp_date.getDate ()! = ParseFloat (day )){
Return false;
} Else {
Return true;
}
}
/**
* Verify that the birthday in the 15-digit ID card number is a valid birthday.
* @ Param idCard15 character book id card string
* @ Return
*/
Function isValidityBrithBy15IdCard (idCard15 ){
Var year = idCard15.substring (6, 8 );
Var month = idCard15.substring (8, 10 );
Var day = idCard15.substring (10, 12 );
Var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day ));
// Use the getYear () method if you are older than your ID card.
If (temp_date.getYear ()! = ParseFloat (year)
| Temp_date.getMonth ()! = ParseFloat (month)-1
| Temp_date.getDate ()! = ParseFloat (day )){
Return false;
} Else {
Return true;
}
}
// Remove the leading and trailing spaces of the string
Function trim (str ){
Return str. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Function showBirthdayAndSex (idCard, sexId, birhId)
{
Var birthdayValue;
Var val = trim (idCard );
If (15 = val. length)
{// 15-digit ID card number
BirthdayValue = val. charAt (6) + val. charAt (7 );
If (parseInt (birthdayValue) <10)
{
BirthdayValue = '20' + birthdayValue;
}
Else
{
BirthdayValue = '19' + birthdayValue;
}
BirthdayValue = birthdayValue + '-' + val. charAt (8) + val. charAt (9) + '-' + val. charAt (10) + val. charAt (11 );
If (parseInt (val. charAt (14)/2) * 2! = Val. charAt (14 ))
{
Document. getElementById (sexId). value = 'male ';
}
Else
{
Document. getElementById (sexId). value = 'femal ';
}
Document. getElementById (birhId). value = birthdayValue;
}
If (18 = val. length)
{// 18-digit ID card number
BirthdayValue = val. charAt (6) + val. charAt (7) + val. charAt (8) + val. charAt (9) + '-' + val. charAt (10) + val. charAt (11) + '-' + val. charAt (12) + val. charAt (13 );
If (parseInt (val. charAt (16)/2) * 2! = Val. charAt (16 ))
{
Document. getElementById (sexId). value = 'male ';
}
Else
{
Document. getElementById (sexId). value = 'femal ';
}
Document. getElementById (birhId). value = birthdayValue;
}
}
// Check the last digit of the 18-digit ID card number
Function IDCard (Num)
{
If (Num. length! = 18)
Return false;
Var x = 0;
Var y = '';
For (I = 18; I> = 2; I --)
X = x + (square (2, (I-1) % 11) * parseInt (Num. charAt (19-i-1 ));
X % = 11;
Y = 12-x;
If (x = 0)
Y = '1 ';
If (x = 1)
Y = '0 ';
If (x = 2)
Y = 'X ';
Return y;
}
// Obtain the power y of x
Function square (x, y)
{
Var I = 1;
For (j = 1; j <= y; j ++)
I * = x;
Return I;
}
Html
<Html>
<Head>
<Title> CordBirthday </title>
<Script language = "javascript" src = "jquery-1.4.1.min.js"> </script>
<Script language = "javascript" src = "IDValidate. js"> </script>
<Script language = "javascript">
$ (Function (){
$ ('# IdCard'). blur (function (){
Var id = $ (this). val ();
If (IdCardValidate (id )){
ShowBirthdayAndSex (id, "sex", "birthday ");
}
Else {
$ ('# Sex'). val ("");
$ ('# Birthday'). val ("");
Alert ("the ID card number you entered is incorrect ");
Return false;
}
});
});
</Script>
</Head>
<Body>
<Form>
<Table align = "center">
<Tr>
<Td align = "center"> id card <input type = "text" id = "idCard"> </td>
</Tr>
<Tr>
<Td align = "center"> gender <input type = "text" id = "sex"> </td>
</Tr>
<Tr>
<Td align = "center"> birthday <input type = "text" id = "birthday"> </td>
</Tr>
<Tr>
<Td align = "center"> <input type = "button" value = "OK"> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>
From Xu Yue's column