Use the Delphi function to determine whether an 18-digit ID card number is valid

Source: Internet
Author: User



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

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.