Java Inspection Bank card number

Source: Internet
Author: User

  1. /*
  2. Verification process:
  3. 1. Starting with the last digit of the card number, the inverse adds the odd digits (1, 3, 5, and so on).
  4. 2, starting from the last digit of the card number, the inverse of the even digit number, first multiplied by 2 (if the product is two digits, the bit 10 digits are added, will be minus 9), and then summed.
  5. 3, the sum of odd digits plus even digits sum, the result should be divisible by 10.
  6. */
  7. /**  
  8. * Verify the bank card number
  9. */
  10. public Static boolean checkbankcard (String bankcard) {
  11. if (Bankcard.length () < | | bankcard.length () > ) {
  12. return false;
  13. }
  14. char bit = Getbankcardcheckcode (bankcard.substring (0, Bankcard.length ()- 1));
  15. if (bit = = ' N ') {
  16. return false;
  17. }
  18. return Bankcard.charat (Bankcard.length ()- 1) = = bit;
  19. }
  20. /**  
  21. * The check digit is obtained from the bank card number without check digit using LUHM check algorithm.
  22. * @param noncheckcodebankcard
  23. * @return
  24. */
  25. public static char getbankcardcheckcode (String noncheckcodebankcard) {
  26. if (Noncheckcodebankcard = = Null | | Noncheckcodebankcard.trim (). Length () = = 0
  27. || !noncheckcodebankcard.matches ("\\d+")) {
  28. //If the data passed is not returned by N
  29. return ' N ';
  30. }
  31. char[] CHS = Noncheckcodebankcard.trim (). ToCharArray ();
  32. int luhmsum = 0;
  33. for (int i = chs.length- 1, j = 0; I >= 0; I--, j + +) {
  34. int k = chs[i]- ' 0 ';
  35. if (j% 2 = = 0) {
  36. K *= 2;
  37. K = k/ ten + k% 10;
  38. }
  39. Luhmsum + = k;
  40. }
  41. return (luhmsum% = = 0)?    ' 0 ': (char) ((10-luhmsum% ) + ' 0 ');
  42. }

Java Inspection Bank card number

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.