Credit Card Mod10 校验

本文介绍了一种用于验证信用卡号码有效性的Luhn算法(MOD10)。通过实例详细展示了该算法的具体步骤,并提供了四种不同的实现方式。此外,还列举了常见信用卡类型及其前缀和长度。
部署运行你感兴趣的模型镜像

以下是几种Mod10的实现。第一种最为简洁,最后一种最为易懂。你喜欢哪一种?

    /**

     * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

     * <br/>

     * (Getting from http://en.wikipedia.org/wiki/Luhn_algorithm)<br/>

     * 1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.<br/>

     * 2. Sum the digits of the products together with the undoubled digits from the original number.<br/>

     * 3. If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number

     *    is valid according to the Luhn formula; else it is not valid.<br/>

     * <br/>

     * As an illustration, if the account number is 49927398716, it will be validated as follows:<br/>

     * i.   Double every second digit, from the rightmost:<br/>

     *     (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18<br/>

     * ii.  Sum all the individual digits (digits in parentheses are the products from Step 1):<br/>

     *      6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70<br/>

     * iii. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.<br/>

     *

     * @param num

     * @return boolean true-valid; false-invalid

     */

    public static boolean isValidCC(String num) {

//          //Check digits

//          if(!num.matches("^\\d{13,19}$"))

//                return false;

           

            //Check mod10

            final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}};

            int sum = 0, flip = 0;

           

            for (int i = num.length() - 1; i >= 0; i--, flip++){

                  int n = num.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                  sum += sumTable[flip & 0x1][n];

            }

            return (sum == 0) ? false : (sum % 10 == 0);

        }

     

    /**

     * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

     * Used switch to double specified value.<br/>

     *

     * @param number

     * @return

     */

      public static boolean isValidCC1(String number) {

            int sum = 0;

           

            int mul = 1;

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                 

                  n *= (mul == 1) ? mul++ : mul--;

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

     

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used position to double specified value.<br/>

       *

       * @param number

       * @return

       */

      public static boolean isValidCC2(String number) {

            int sum = 0;

           

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

 

                  if ((number.length()-i)%2 == 0)

                        n *= 2;

 

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

     

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used AND and XOR to double specified value.<br/>

       * @param number

       * @return

       */

      public static boolean isValidCC3(String number) {

            int digits = number.length();

            int oddOrEven = digits & 1;

            long sum = 0;

            for (int i = 0; i < digits; i++) {

                  int n = number.charAt(i) - '0';

                  if (n < 0 || n > 9)

                        return false;

                 

                  if (((i & 1) ^ oddOrEven) == 0)

                        n *= 2;

                  sum += (n>9 ? n-9 : n);

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

 

      /**

       * Check whether a credit card number is valid or not according to the Luhn algorithm (MOD10).<br/>

       * Used a variable as switch to double specified value.

       * @param number

       * @return

       */

      private static boolean isValidCC4(String number) {

            int sum = 0;

 

            boolean alternate = false;

            for (int i = number.length() - 1; i >= 0; i--) {

                  int n = Integer.parseInt(number.substring(i, i + 1)); //May throw out a runtime exception

                  if (n < 0 || n > 9)

                        return false;

                  if (alternate)

                        n *= 2;

                  sum += (n>9 ? n%10 + 1 : n);

                  alternate = !alternate;

            }

 

            return (sum == 0) ? false : (sum % 10 == 0);

      }

 

信用卡前缀及校验规则

CARD TYPEPrefixLengthCheck digit algorithm
MASTERCARD51-5516mod 10
VISA413, 16mod 10
AMEX34 
37
15mod 10
Diners Club/
Carte Blanche
300-305
36
38
14mod 10
Discover601116mod 10
enRoute2014
2149
15any
JCB316mod 10
JCB2131
1800
15mod 10

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

项目2_1:信用卡验证 问题描述 信用卡号遵循特定规则,需满足以下条件: - 位数在13到16位之间; - 起始数字需符合对应卡种要求: - 维萨卡(Visa)以4开头; - 万事达卡(Master)以5开头; - 美国运通卡(American Express)以37开头; - 发现卡(Discover)以6开头。 1954年,IBM的汉斯·卢恩(Hans Luhn)提出了一种信用卡号验证算法,该算法可用于判断卡号输入是否正确,或扫描仪扫描信用卡是否准确。几乎所有信用卡号的生成都遵循这一有效性校验规则,该规则通常被称为“卢恩校验”(Luhn Check)或“模10校验”(Mod 10 Check),具体步骤如下(以卡号4388576018402626为例说明): 1. 从右往左,将每隔一位的数字翻倍。若翻倍后结果为两位数,则将这两位数字相加,得到一个个位数。 - 2×2 = 4 - 2×2 = 4 - 4×2 = 8 - 1×2 = 2 - 6×2 = 12(1 + 2 = 3) - 5×2 = 10(1 + 0 = 1) - 8×2 = 16(1 + 6 = 7) - 4×2 = 8 2. 将步骤1中得到的所有个位数相加。 - 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 3. 从右往左,将卡号中位于奇数位置的所有数字相加。 - 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38 4. 将步骤2和步骤3的结果相加。 - 37 + 38 = 75 5. 若步骤4的结果能被10整除,则该信用卡号有效;否则无效。例如,卡号4388576018402626无效,而卡号4388576018410707有效。 编写一个程序,提示用户输入一个长整数类型的信用卡号,然后输出该卡号是否有效。 程序运行示例 示例1: 输入长整数类型的信用卡号:4246345689049834 4246345689049834 无效
最新发布
09-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值