JavaScript or ASP to determine whether the identity card number is correct two methods of verification _javascript skills

Source: Internet
Author: User
Tags ereg
The specific code is as follows:

First, with JavaScript validation:
Copy Code code as follows:

<script>
function Checkidcard (idcard) {
var errors=new Array (
"Verify through!",
"ID card number is wrong!",
"ID number date of birth out of range or contain illegal characters!",
"ID card number checksum error!",
"The identity card area is illegal!"
);
var area={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"}

var Idcard,y,jym;
var s,m;
var idcard_array = new Array ();
Idcard_array = Idcard.split ("");
Regional inspection
if (Area[parseint (Idcard.substr (0,2))]==null) return errors[4];
Identification number and format test
Switch (idcard.length) {
Case 15:
if ((parseint (Idcard.substr (6,2)) +1900% 4 = 0 | | ((parseint (Idcard.substr (6,2)) +1900% = = 0 && (parseint (Idcard.substr (6,2)) +1900)% 4 = 0)) {
Ereg=/^[1-9][0-9]{5}[0-9]{2} ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}$/;//Test Birth date legality
} else {
Ereg=/^[1-9][0-9]{5}[0-9]{2} ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}$/;//test date of birth legality
}
if (Ereg.test (Idcard)) return errors[0];
else return errors[2];
Break
Case 18:
18-digit identification number detection
Validity of Birth date check
Leap Month Day: ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]))
Flat Month Day: ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8])
if (parseint (Idcard.substr (6,4))% 4 = 0 | | (parseint (Idcard.substr (6,4))% = = 0 && parseint (idcard.substr (6,4))%4 = = 0)) {
Ereg=/^[1-9][0-9]{5}19[0-9]{2} ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}[0-9xx]$/;//validity of a leap year birth date regular expression
} else {
Ereg=/^[1-9][0-9]{5}19[0-9]{2} ((01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}[0-9xx]$/;//excepting the legality of the date of birth regular expression
}
if (Ereg.test (Idcard)) {//test date of birth legality
Calculate Check digit
S = (parseint (idcard_array[0]) + parseint (idcard_array[10)) * 7
+ (parseint (idcard_array[1]) + parseint (idcard_array[11)) * 9
+ (parseint (idcard_array[2]) + parseint (idcard_array[12)) * 10
+ (parseint (idcard_array[3]) + parseint (idcard_array[13)) * 5
+ (parseint (idcard_array[4]) + parseint (idcard_array[14)) * 8
+ (parseint (idcard_array[5]) + parseint (idcard_array[15)) * 4
+ (parseint (idcard_array[6]) + parseint (idcard_array[16)) * 2
+ parseint (idcard_array[7]) * 1
+ parseint (idcard_array[8]) * 6
+ parseint (idcard_array[9]) * 3;
Y = S% 11;
M = "F";
Jym = "10x98765432";
M = Jym.substr (y,1);//Judge check digit
if (M = = idcard_array[17]) return errors[0]; Check digit of Detection ID
else return errors[3];
}
else return errors[2];
Break
Default
return errors[1];
Break
}

}
Alert (Checkidcard ("220183198808081610"))
</script>

Second, through the ASP code validation:

Copy Code code as follows:

<%
Function Checkidcard (Idcard) '-1 for the correct identity card, otherwise for illegal ID

Dim Y, Jym
Dim S, M

Dim Area
Area = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91 "
Dim Ereg
Set Ereg = New regexp

' Area test
If InStr (1, area, Mid (Idcard, 1, 2)) = 0 Then Checkidcard = 1:exit Function

' ID number and format test
Select case Len (Idcard)
Case 15
If ((CInt (Idcard, 7, 2) + 1900) MoD 4 = 0 or ((CInt (Mid (Idcard, 7, 2)) + 1900) mod = 0 and (CInt (mid Idcard, 7, 2 ) + 1900) Mod 4 = 0) Then
Ereg. Pattern = "^[1-9][0-9]{5}[0-9]{2}" (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}$]/test date of birth legality
Else
Ereg. Pattern = "^[1-9][0-9]{5}[0-9]{2}" (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}$ ';//validity of the test date of birth
End If
If (Ereg.test (idcard)) Then
Checkidcard =-1
Else
Checkidcard = 2
End If
Case 18
'//18-digit identification number detection
'//Birth date legality check
If ((CInt (Idcard, 7, 2) + 1900) MoD 4 = 0 or ((CInt (Mid (Idcard, 7, 2)) + 1900) mod = 0 and (CInt (mid Idcard, 7, 2 ) + 1900) Mod 4 = 0) Then
Ereg. Pattern = "^[1-9][0-9]{5}19[0-9]{2}" (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|[ 1-2][0-9]) [0-9]{3}[0-9xx]$ "]//the legality of the date of birth of a leap year regular expression
Else
Ereg. Pattern = "^[1-9][0-9]{5}19[0-9]{2}" (01|03|05|07|08|10|12) (0[1-9]|[ 1-2][0-9]|3[0-1]) | (04|06|09|11) (0[1-9]| [1-2] [0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) [0-9]{3}[0-9xx]$ ';//excepting the legality of the date of birth regular expression of
End If
If (Ereg.test (idcard)) Then
'//COMPUTE parity bit
S = (CInt (Mid (Idcard, 0 + 1, 1)) + CInt (Mid (Idcard, 10 + 1, 1)) * 7 _
+ (CInt (Mid (Idcard, 1 + 1, 1)) + CInt (Mid (Idcard, 11 + 1, 1)) * 9 _
+ (CInt (Mid (Idcard, 2 + 1, 1)) + CInt (Mid (Idcard, 12 + 1, 1)) * 10 _
+ (CInt (Mid (Idcard, 3 + 1, 1)) + CInt (Mid (Idcard, 13 + 1, 1)) * 5 _
+ (CInt (Mid (Idcard, 4 + 1, 1)) + CInt (Mid (Idcard, 14 + 1, 1)) * 8 _
+ (CInt (Mid (Idcard, 5 + 1, 1)) + CInt (Mid (Idcard, 15 + 1, 1)) * 4 _
+ (CInt (Mid (Idcard, 6 + 1, 1)) + CInt (Mid (Idcard, 16 + 1, 1)) * 2 _
+ CInt (Mid (Idcard, 7 + 1, 1)) * 1 _
+ CInt (Mid (Idcard, 8 + 1, 1)) * 6 _
+ CInt (Mid (Idcard, 9 + 1, 1)) * 3
Y = S Mod 11
M = "F"
Jym = "10x98765432"
M = Mid (jym, Y + 1, 1)

If (M = Mid (Idcard, 1, 1)) Then Checkidcard =-1 Else checkidcard = 3

Else
Checkidcard = 4
End If

Case Else
Checkidcard = Len (Idcard)
End Select

End Function%>
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.