Javascript birth date and ID card judgment Daquan _ time date

Source: Internet
Author: User
The identification card and date of birth are all included. The date of birth cannot be blank. If the date of birth is not blank, the date of birth must be 8 digits. If the ID card number is 18 or 15 digits, it must match the date of birth; if it is a military official ID, it must start with a Chinese character! If (document. form1.csrq. value = ""){
Alert ("Enter the date of birth! ");
Document. form1.csrq. focus ();
Return false;
}
If (document. form1.csrq. value = ""){
Alert ("Enter the date of birth! ");
Document. form1.csrq. focus ();
Return false;
} Else {
If (document. form1.csrq. value. length! = 8 ){
Alert ("the birth date must be 8 digits! ");
Document. form1.csrq. focus ();
Return false;
}
If (document. form1.sfzh. value. length = 18 ){
Var sfzh = document. form1.sfzh. value;
If (document. form1.csrq. value! = Sfzh. substring (6, 14 )){
Alert ("the date of birth is inconsistent with the ID card number! ");
Document. form1.csrq. focus ();
Return false;
}
}
If (document. form1.sfzh. value. length = 15 ){
Var sfzh = document. form1.sfzh. value;
If (document. form1.csrq. value! = '19' + sfzh. substring (6, 12 )){
Alert ("the date of birth is inconsistent with the ID card number! ");
Document. form1.csrq. focus ();
Return false;
}
}
}
If (! IsIdCardNo (document. form1.sfzh. value )){
Document. form1.sfzh. focus ();
Return false;
}
Function isNN (num ){
Var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // uppercase letter
BadChar + = "abcdefghijklmnopqrstuvwxyz"; // lowercase letter
BadChar + = "0123456789"; // number
BadChar + = "" + ""; // halfwidth and fullwidth Spaces
BadChar + = "'~! @ # $ % ^ & ()-_ = +] \\| :;\" \\' <,>? /"; // Special symbol
Var c;
// For (var I = 0; I C = num. charAt (0); // extracts the 1st characters in num.
If (badChar. indexOf (c )! =-1) {// if the current character matches a character in badChar, it is not a Chinese character
Return false;
} Else {
Return true;
}
}
Function isIdCardNo (num)
{
// If (isNaN (num) {alert ("the input is not a number! "); Return false ;}
Var len = num. length, re;
If (len = 15 ){
Re = new RegExp (/^ (\ d {6 })()? (\ D {2}) (\ d {2}) (\ d {2}) (\ d {3}) $ /);
} Else if (len = 18 ){
Re = new RegExp (/^ (\ d {6 })()? (\ D {4}) (\ d {2}) (\ d {2}) (\ d {3}) (\ d) $ /);
} Else if (isNN (num )){
Return true;
} Else {
Alert ("if the officer ID is complete, for example, a military officer ID is Zheng Zi 123,456th. If it is an ID card number, check the number of digits you entered. It must be 15 or 18 digits. ");
Return false;
}
Var a = num. match (re );
If (! = Null)
{
If (len = 15)
{
Var D = new Date ("19" + a [3] + "/" + a [4] + "/" + a [5]);
Var B = D. getYear () = a [3] & (D. getMonth () + 1) = a [4] & D. getDate () = a [5];
}
If (len = 18)
{
Var D = new Date (a [3] + "/" + a [4] + "/" + a [5]);
Var B = D. getFullYear () = a [3] & (D. getMonth () + 1) = a [4] & D. getDate () = a [5];
}
If (! B) {alert ("entered ID card number" + a [0] + "contains incorrect birthdate! "); Return false ;}
}
Return true;
}
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.