是否以字母开头
- (BOOL)fromLettersStart:(NSString *)string {
if(string.length < 1) {
return NO;
}
NSString *firstStr = [string substringToIndex:1];
NSString regex = @"[a-zA-Z]";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject: firstStr];
}
是否以汉字开头
- (BOOL)fromChineseStart:(NSString *)string {
if(string.length < 1) {
return NO;
}
NSString *firstStr = [string substringToIndex:1];
NSString *regex = @"[\u4e00-\u9fa5]+";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject: firstStr];
}
是否是手机号
- (BOOL)isMobilePhone:(NSString )string {
if(string.length != 11) {
return NO;
}
/*
* 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
* 联通号段: 130,131,132,155,156,185,186,145,176,1709
* 电信号段: 133,153,180,181,189,177,1700
*/
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\d{8}$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [pre evaluateWithObject: string];
}
银行卡号有效性
+ (BOOL)isBankCardNumber:(NSString *)string {
if(string.length < 10) {
return NO;
}
NSString * lastNum = [[string substringFromIndex:(string.length-1)] copy];
NSString * forwardNum = [[string substringToIndex:(string.length -1)] copy];
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--) {
[forwardDescArr addObject:forwardArr[i]];
}
NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];
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:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumOddNumTotal += [obj integerValue];
}];
__block NSInteger sumOddNum2Total = 0;
[arrOddNum2 enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumOddNum2Total += [obj integerValue];
}];
__block NSInteger sumEvenNumTotal =0 ;
[arrEvenNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
sumEvenNumTotal += [obj integerValue];
}];
NSInteger lastNumber = [lastNum integerValue];
NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal;
return (luhmTotal%10 ==0)?YES:NO;
}