• iOS银行卡合法性校验


    项目中用到了校验银行卡,就拿来贴上来了 

    + (BOOL)checkCardNo:(NSString*)cardNo;//判断银行卡

    + (BOOL)checkCardNo:(NSString*)cardNo{

        int oddsum = 0;     //奇数求和

        int evensum = 0;    //偶数求和

        int allsum = 0;

        int cardNoLength = (int)[cardNo length];

        int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];

        

        cardNo = [cardNo substringToIndex:cardNoLength - 1];

        for (int i = cardNoLength -1 ; i>=1;i--) {

            NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];

            int tmpVal = [tmpString intValue];

            if (cardNoLength % 2 ==1 ) {

                if((i % 2) == 0){

                    tmpVal *= 2;

                    if(tmpVal>=10)

                        tmpVal -= 9;

                    evensum += tmpVal;

                }else{

                    oddsum += tmpVal;

                }

            }else{

                if((i % 2) == 1){

                    tmpVal *= 2;

                    if(tmpVal>=10)

                        tmpVal -= 9;

                    evensum += tmpVal;

                }else{

                    oddsum += tmpVal;

                }

            }

        }

        

        allsum = oddsum + evensum;

        allsum += lastNum;

        if((allsum % 10) == 0)

            return YES;

        else

            return NO;

    }

  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5783773.html
Copyright © 2020-2023  润新知