18-digit ID card Verification Algorithm

Source: Internet
Author: User
ID card verification code algorithm
ID card verification code generation method:
SIGMA (AI x WI) (mod 11)

I: the serial number of the character from the start to the left, including the verification code;

AI indicates the number character value at position I;

The weighted factor at the I position of wi. Its value is Wi = Mod (Power (2, (n-1), 11)

I 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

Check Code:
SIGMA (AI x WI) (mod 11) 0 1 2 3 4 5 6 7 8 9 10
Verification Code character value: AI 1 0x9 8 7 6 5 4 3 2

  1. Create or replace function id_15to18 (idcardnum varchar2) return varchar2 is
  2. Result varchar2 (20 );
  3. I smallint;
  4. Num smallint: = 0;
  5. Snum varchar2 (2 );
  6. Code char (1 );
  7. Temp17 varchar2 (20 );
  8. Begin
  9. If length (idcardnum) = 18 or idcardnum is null then
  10. Result: = upper (idcardnum );
  11. Elsif length (idcardnum) = 15 then
  12. Temp17: = substr (idcardnum, 1, 6) | '19' | substr (idcardnum, 7, 9 );
  13. I: = 18;
  14. While I> 1 Loop
  15. Num: = num + Mod (Power (2, I-1), 11) * substr (temp17, 19-I, 1 );
  16. I: = I-1;
  17. End loop;
  18. Num: = Mod (Num, 11 );
  19. Snum: = num | '';
  20. If num = 0 then
  21. Code: = '1 ';
  22. Elsif num = 1 then
  23. Code: = '0 ';
  24. Elsif num = 2 then
  25. Code: = 'X ';
  26. Else
  27. Code: = (12-num) | '';
  28. End if;
  29. Result: = temp17 | code;
  30. Else
  31. Result: = idcardnum;
  32. End if;
  33. Return result;
  34. Exception
  35. When others then
  36. Dbms_output.put_line ('erroe '| idcardnum );
  37. End id_15to18;

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.