Paip. Effectiveness check of bank card numbers

Source: Internet
Author: User

Paip. Effectiveness check of bank card numbers

No matter whether it is 13, 16, 19 bank card numbers are calculated using the luhm verification algorithm, which is ISO standard.

The second digit is the verification code.

The luhm verification method is used to calculate the non-bits:

1. Numbers 1 to XX from the Right, and numbers on the odd number multiplied by 2
2. Add all the ten digits of the odd-bits product and all the numbers on the even-bits.
3. The addition and addition of the check bit can be divided by 10.

For example, the card number:

Java code

11 6 2 2 5 8 8 1 4 4 2 0 7 4 3
* 2 2 2 2 2 2 2 2
--------------------------------------------------
12 2 4 5 16 8 2 4 4 4 4 6
 
Add the preceding numbers: 1 + 2 + 2 + 4 + 5 + 1 + 6 + 8 + 2 + 4 + 2 + 4 + 4 + 1 + 4 + 4 + 6 = 60
 
Since 60 and 0 can be divisible by 10, the check bit is 0.
 
Therefore, the card number is 6225 8814 1420 7430

 

If one of the digits is replaced, the last check bit is incorrect.

I wrote a program for computation:

Java code
Come
42 public class test5 {
 
Public static void main (string [] ARGs ){
String card = "622588141420743 ";
System. Out. println ("card:" + card );
System. Out. println ("Check Code:" + getbankcardcheckcode (card ));
System. Out. println ("card ID:" + card + getbankcardcheckcode (card ));
}

/**
* Verify the bank card number
* @ Param cardid
* @ Return
*/
Public static Boolean checkbankcard (string cardid ){
Char bit = getbankcardcheckcode (cardid. substring (0, cardid. Length ()-1 ));
Return cardid. charat (cardid. Length ()-1) = bit;
}

/**
* Use the luhm Verification Algorithm to obtain the verification digit from the bank card number that does not contain the verification Digit
* @ Param noncheckcodecardid
* @ Return
*/
Public static char getbankcardcheckcode (string noncheckcodecardid ){
If (noncheckcodecardid = NULL | noncheckcodecardid. Trim (). Length () = 0
|! Noncheckcodecardid. Matches ("\ D + ")){
Throw new illegalargumentexception ("bank card code must be number! ");
}
Char [] CHS = noncheckcodecardid. Trim (). tochararray ();
Int luhmsum = 0;
For (INT I = CHS. Length-1, j = 0; I> = 0; I --, J ++ ){
Int K = CHS [I]-'0 ';
If (J % 2 = 0 ){
K * = 2;
K = K/10 + K % 10;
}
Luhmsum + = K;
}
Return (luhmsum % 10 = 0 )? '0': (char) (10-luhmsum % 10) + '0 ');
}

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.