經常使用正則表達式

/**
 *  手機號碼驗證
 *
 *  @param mobileNumbel 傳入的手機號碼
 *
 *  @return 格式正確返回YES  錯誤 返回 NO
 */
+ (BOOL) isMobile:(NSString *)mobileNumbel
{
    /**
     * 手機號碼
     * 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 聯通:130,131,132,152,155,156,185,186
     * 電信:133,1349,153,180,189,181(增長)
     * 新增:14_,17_
     */
    NSString * MOBIL = @"^1(3[0-9]|4[0-9]|5[0-35-9]|7[0-9]|8[025-9])\\d{8}$";
    /**
     10         * 中國移動:China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中國聯通:China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中國電信:China Telecom
     21         * 133,1349,153,180,189,181(增長)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";

    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }

    return NO;
}
/**
 *  密碼驗證
 *
 *  @param password 傳入的密碼 6-16位 大小寫數字
 *
 *  @return 格式正確返回YES  錯誤 返回 NO
 */
+ (BOOL) isPassword:(NSString *)password
{

    NSString *pwd = @"^[a-zA-Z0-9]{6,16}$";

    NSPredicate *regextestPwd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pwd];

    if ([regextestPwd evaluateWithObject:password]) {

        return YES;
    }
    return NO;
}
/**
 *  郵箱驗證
 *
 *  @param email 傳入的郵箱
 *
 *  @return 格式正確返回YES  錯誤 返回 NO
 */
+ (BOOL) isEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    if ([emailTest evaluateWithObject:email]) {

        return YES;
    }
    return NO;
}
/**
 *  身份證號驗證
 *
 *  @param identityCard 傳入的身份證號
 *
 *  @return 格式正確返回YES  錯誤 返回 NO
 */
+ (BOOL) isIdentityCard: (NSString *)identityCard
{

    NSString *idnum = @"^(\\d{14}|\\d{17})(\\d|[xX])$";

    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",idnum];

    return [identityCardPredicate evaluateWithObject:identityCard];
}
/** 銀行卡號有效性問題Luhn算法
 *  現行 16 位銀聯卡現行卡號開頭 6 位是 622126~622925 之間的,7 到 15 位是銀行自定義的,
 *  多是髮卡分行,髮卡網點,髮卡序號,第 16 位是校驗碼。
 *  16 位卡號校驗位採用 Luhm 校驗方法計算:
 *  1,將未帶校驗位的 15 位卡號從右依次編號 1 到 15,位於奇數位號上的數字乘以 2
 *  2,將奇位乘積的個十位所有相加,再加上全部偶數位上的數字
 *  3,將加法和加上校驗位能被 10 整除。
 */
+ (BOOL) isBankCardluhmCheck:(NSString *)bankNumber
{
    NSString * lastNum = [[bankNumber substringFromIndex:(bankNumber.length-1)] copy];//取出最後一位
    NSString * forwardNum = [[bankNumber substringToIndex:(bankNumber.length -1)] copy];//前15或18位

    NSMutableArray * forwardArr = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i=0; i<forwardNum.length; i++) {
        NSString * subStr = [forwardNum substringWithRange:NSMakeRange(i, 1)];
        [forwardArr addObject:subStr];
    }

    NSMutableArray * forwardDescArr = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = (int)(forwardArr.count-1); i> -1; i--) {//前15位或者前18位倒序存進數組
        [forwardDescArr addObject:forwardArr[i]];
    }

    NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];//奇數位*2的積 < 9
    NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];//奇數位*2的積 > 9
    NSMutableArray * arrEvenNum = [[NSMutableArray alloc] initWithCapacity:0];//偶數位數組

    for (int i=0; i< forwardDescArr.count; i++) {
        NSInteger num = [forwardDescArr[i] intValue];
        if (i%2) {//偶數位
            [arrEvenNum addObject:[NSNumber numberWithInteger:num]];
        }else{//奇數位
            if (num * 2 < 9) {
                [arrOddNum addObject:[NSNumber numberWithInteger:num * 2]];
            }else{
                NSInteger decadeNum = (num * 2) / 10;
                NSInteger unitNum = (num * 2) % 10;
                [arrOddNum2 addObject:[NSNumber numberWithInteger:unitNum]];
                [arrOddNum2 addObject:[NSNumber numberWithInteger:decadeNum]];
            }
        }
    }

    __block  NSInteger sumOddNumTotal = 0;
    [arrOddNum enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        sumOddNumTotal += [obj integerValue];
    }];


    __block NSInteger sumOddNum2Total = 0;
    [arrOddNum2 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        sumOddNum2Total += [obj integerValue];
    }];

    __block NSInteger sumEvenNumTotal = 0 ;
    [arrEvenNum enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        sumEvenNumTotal += [obj integerValue];
    }];


    NSInteger lastNumber = [lastNum integerValue];

    NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal;

    return (luhmTotal%10 ==0)?YES:NO;
}
@end
相關文章
相關標籤/搜索