Uses dateutils;
Const
Intmultiplication: array [1 .. 17] of integer = (, 2 );
// Function name: isrightid
// Parameter: Sid, ID card number (input)
// Ssex, gender (output)
// Age, age (output)
// Return value: Sid is a standard ID card number, and OK is returned; otherwise, corresponding information is returned.
Function isrightid (SID: string; var ssex: string; var age: integer): string;
// Determine whether an invalid character exists
Function judgeillegal (STR: string): Boolean;
VaR
I: integer;
Begin
Result: = false;
For I: = 1 to length (STR) Do
Begin
Case STR [I] of '0'... '9 ':
Begin
End
Else
Begin
Exit;
End;
End;
End;
Result: = true;
End;
// Weighting Function
Function weighted (STR: string): string;
VaR
I: integer;
Sum: integer;
Begin
Sum: = 0;
For I: = 1 to 17 do
Begin
Sum: = sum + strtoint (STR [I]) * intmultiplication [I];
End;
Sum: = sum mod 11;
Case sum
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;
Begin
Result: = '';
Try
If length (SID) <> 18 then
Begin
Result: = 'id card number should be 18 characters! ';
Exit;
End;
If not judgeillegal (copy (SID, 1, 17) then
Begin
Result: = 'the first 17 digits of the ID card number should be numbers! ';
Exit;
End;
// Judgment successful
If (weighted (SID) = copy (SID, 18, 1) then
Begin
Result: = 'OK ';
// Determine gender
If (strtoint (copy (SID, 17,1) mod 2) = 0 then
Ssex: = 'female'
Else
Ssex: = 'male ';
// Calculate the age
Age: = yearof (now)-strtoint (copy (SID, 7,4 ));
End
Else
Begin
Result: = 'incorrect ID card number! ';
End
Except
On E: exception do
Begin
Result: = 'exception: '+ E. message;
End;
End;
End;
Use the Delphi function to determine whether an 18-digit ID card number is valid