Green function: verify ID card number Validity

Source: Internet
Author: User
Tags idate
{Verify the validity of the ID card number. If the returned value is null, it indicates normal. Otherwise, it indicates an error message}
{Author: Moon night kite, edocu@163.com}
Function validatepid (const apid: string): string;
{Internal function, which is the last digit of the ID card number, valid for 18 digits}
Function getverifybit (sidentitynum: string): Char;
VaR
Nnum: integer;
Begin
Result: = #0;
Nnum: = strtoint (sidentitynum [1]) * 7 +
Strtoint (sidentitynum [2]) * 9 +
Strtoint (sidentitynum [3]) x 10 +
Strtoint (sidentitynum [4]) x 5 +
Strtoint (sidentitynum [5]) x 8 +
Strtoint (sidentitynum [6]) x 4 +
Strtoint (sidentitynum [7]) x 2 +
Strtoint (sidentitynum [8]) * 1 +
Strtoint (sidentitynum [9]) x 6 +
Strtoint (sidentitynum [10]) x 3 +
Strtoint (sidentitynum [11]) * 7 +
Strtoint (sidentitynum [12]) * 9 +
Strtoint (sidentitynum [13]) * 10 +
Strtoint (sidentitynum [14]) x 5 +
Strtoint (sidentitynum [15]) x 8 +
Strtoint (sidentitynum [16]) x 4 +
Strtoint (sidentitynum [17]) * 2;
Nnum: = nnum mod 11;
Case nnum
0: Result: = '1 ';
1: Result: = '0 ';
2: Result: = 'X ';
3: Result: = '9 ';
4: Result: = '8 ';
5: Result: = '7 ';
6: Result: = '6 ';
7: Result: = '5 ';
8: Result: = '4 ';
9: Result: = '3 ';
10: Result: = '2 ';
End;
End;
VaR
L: integer;
Scentury: string;
Syear2bit: string;
Smonth: string;
Sdate: string;
Icentury: integer;
Imonth: integer;
Idate: integer;
Crcfact: string; // the actual value of the 18-digit certificate number
Crcth: string; // theoretical value of the 18-digit certificate number
Febdayamt: byte; // day of the month
Begin
L: = length (apid );
If (L in [15, 18]) = false then
Begin
Result: = format ('Id card number is not 15 or 18 bits (% 0: S, actual bits: % 1: d) ', [apid, l]);
Exit;
End;
Crcfact: = '';
If l = 18 then
Begin
Scentury: = copy (apid, 7, 2 );
Icentury: = strtoint (scentury );
If (icentury in [18 .. 20]) = false then
Begin
Result: = format ('invalid ID number: The first two digits of the 18-digit ID number must be between 18-20 (% 0: s) ', [scentury]);
Exit;
End;
Syear2bit: = copy (apid, 9, 2 );
Smonth: = copy (apid, 11, 2 );
Sdate: = copy (apid, 13, 2 );
Crcfact: = copy (apid, 18, 1 );
End else
Begin
Scentury: = '19 ';
Syear2bit: = copy (apid, 7, 2 );
Smonth: = copy (apid, 9, 2 );
Sdate: = copy (apid, 11, 2 );
End;
Imonth: = strtoint (smonth );
Idate: = strtoint (sdate );
If (Imonth in [01 .. 12]) = false then
Begin
Result: = format ('invalid ID card number: The month must be between 01-12 (% 0: s) ', [smonth]);
Exit;
End;
If (Imonth in [1, 3, 5, 7, 8, 10, 12]) then
Begin
If (idate in [01 .. 31]) = false then
Begin
Result: = format ('invalid ID card number: Invalid date, cannot be zero or exceed the maximum value of the current month (% 0: s) ', [sdate]);
Exit;
End;
End;
If (Imonth in [4, 6, 9, 11]) then
Begin
If (idate in [01 .. 30]) = false then
Begin
Result: = format ('invalid ID card number: Invalid date, cannot be zero or exceed the maximum value of the current month (% 0: s) ', [sdate]);
Exit;
End;
End;
If isleapyear (strtoint (scentury + syear2bit) = true then
Begin
Febdayamt: = 29;
End else
Begin
Febdayamt: = 28;
End;
If (Imonth in [2]) then
Begin
If (idate in [01 .. febdayamt]) = false then
Begin
Result: = format ('invalid ID card number: Invalid date, cannot be zero or exceed the maximum value of the current month (% 0: s) ', [sdate]);
Exit;
End;
End;
If crcfact <> ''then
Begin
Crcth: = getverifybit (apid );
If crcfact <> crcth then
Begin
Result: = format ('invalid ID card number: Check bit (18th bits) Error :( % 0: s) ', [apid]);
Exit;
End;
End;
End;

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.