JS verification ID card number (including matching verification with user input gender and birthday)

Source: Internet
Author: User

// When submitting a form
Function checktwo (){
// Return checksfz2 ();
// Alert (idcardvalidate (idcard ));
// Alert (maleorfemalbyidcard (idcard ));
// Alert (iscardname ());
// Return false;
If (iscardname () & idcardvalidate () & consistencyvalidate ()){
If (beagreed ()){
VaR agree_msg = $ ('. magree ');
Agree_msg.removeclass ('agreemsg _ R ');
Agree_msg.addclass ('agreemsg ');
Return true;
}
}
Certificate ('.next_msg'mirror.css ('display', 'block ');
Return false;

}

// Select "I agree"

Function beagreed (){
VaR agree = $ ('. Agree ');
VaR agree_msg = $ ('. magree ');
If (agree. ATTR ('checked') = 'checked '){
Agree_msg.removeclass ('agreemsg _ R ');
Agree_msg.addclass ('agreemsg ');
Return true;
}
Agree_msg.removeclass ('agreemsg ');
Agree_msg.addclass ('agreemsg _ R ');
Return false;
}
// Check name: The name is a string of 2 to 15 characters.
Function iscardname ()
{
VaR nameindium = $ ('. name ');
VaR name = $ ('. name'). Val ();
VaR name_empty = $ ('. nameempty ');
If (name = ''){
Name_empty.css ('visibility ', 'visable ');
Return false;
}
VaR patrn =/^ \ s * [\ u4e00-\ u9fa5] {1,} [\ u4e00-\ u9fa5. ·] {0, 15} [\ u4e00-\ u9fa5] {1,} \ s * $ /;
If (! Patrn.exe C (name ))
{
Name_empty.css ('visibility ', 'visable ');
Return false;
}
Name_empty.css ('visibility ', 'den den ');
Return true;
}

// Verify the ID card, birthday, and gender
Function consistencyvalidate (){
// Alert (idcardvalidate ());
VaR idcard = $ ('. idnum'). Val ();
If (idcard! = ''){
If (idcardvalidate ()){
Nian = idcard. substr (6, 4 );
Yue = idcard. substr (10, 2 );
Ri = idcard. substr (12, 2 );
VaR Birthday = $ ('. birthday'). Val ();
// Alert (birthday );
VaR gender = $ ('. gendersel'). Val ();
VaR birthdaycard = Nian + "-" + Yue + "-" + Ri;
VaR gendercard = maleorfemalbyidcard (idcard );
VaR gender_error = $ ('. gendererror ');
VaR birthday_error = $ ('. birthdayerror ');
// Alert (Gender = gendercard );
If (gender! = Gendercard ){
Gender_error.css ('visibility ', 'visable ');
Return false;
} If (Gender = gendercard ){
Gender_error.css ('visibility ', 'den den ');
} If (birthday! = Birthdaycard | Birthday = ''){
Birthday_error.css ('visibility ', 'visable ');
Return false;
} If (Birthday = birthdaycard ){
Birthday_error.css ('visibility ', 'den den ');
}
Return true;
}
}
}
/**
* 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

// Verify the ID card number (18 digits)

Function idcardvalidate (){
// Get the birthday in the ID card
VaR idcard = $ ('. idnum'). Val ();
VaR idnum_error = $ ('. idnumerror ');
Idcard = trim (idcard. Replace (// G ,""));
If (idcard. Length = 18 ){

VaR a_idcard = idcard. Split (""); // obtain the ID card Array

// Call the method to determine whether the last verification bit is correct and whether the birthday is valid

If (isvaliditybrithby18idcard (idcard) & istruevalidatecodeby18idcard (a_idcard )){

Idnum_error.css ('visibility ', 'den den ');
// Consistencyvalidate ();
Return true;
} Else {
Idnum_error.css ('visibility ', 'visable ');
Return false;
}
} Else {
Idnum_error.css ('visibility ', 'visable ');
Return false;
}
}
// Remove the leading and trailing spaces of the string
Function trim (STR ){
Return Str. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}
/**
* 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;
}

}

/**
* 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;
}
}
/**
* 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;
//}
}

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.