// // NSString+Check.m // 拍货郎 // // Created by 刘峰 on 2019/3/22. // Copyright © 2019年 刘峰. All rights reserved. // #import "NSString+Check.h" #import "NSString+YYAdd.h" @implementation NSString (Check) - (BOOL)checkPhone{ if (![self isNotBlank]) { return NO; } if (self.length != 11) { return NO; } NSString *regex = @"^(\\+86-|\\+86|86-|86){0,1}1[1|3|4|5|6|7|8|9]{1}\\d{9}$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isMatch = [pred evaluateWithObject:self]; if (!isMatch){ return NO; } return YES; } - (BOOL)checkBankCard{ int oddSum = 0; // 奇数和 int evenSum = 0; // 偶数和 int allSum = 0; // 总和 // 循环加和 for (NSInteger i = 1; i <= self.length; i++){ NSString *theNumber = [self substringWithRange:NSMakeRange(self.length-i, 1)]; int lastNumber = [theNumber intValue]; if (i%2 == 0){ // 偶数位 lastNumber *= 2; if (lastNumber > 9){ lastNumber -=9; } evenSum += lastNumber; }else{ // 奇数位 oddSum += lastNumber; } } allSum = oddSum + evenSum; // 是否合法 if (allSum%10 == 0){ return YES; }else{ return NO; } } - (BOOL)deptNumInputShouldNumber{ if (self.length == 0) { return NO; } NSString *regex = @"[0-9]*"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; if ([pred evaluateWithObject:self]) { return YES; } return NO; } - (BOOL)isChinese{ NSString *match = @"(^[\u4e00-\u9fa5]+$)"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; return [predicate evaluateWithObject:self]; } - (BOOL)includeChinese{ for(int i=0; i< [self length];i++){ int a =[self characterAtIndex:i]; if( a >0x4e00&& a <0x9fff){ return YES; } } return NO; } //校验身份证 - (BOOL)verifyIDCardNumber { NSString *value = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([value length] != 18) { return NO; } NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))"; NSString *leapMmdd = @"0229"; NSString *year = @"(19|20)[0-9]{2}"; NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])"; NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd]; NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd]; NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"]; NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}"; NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd , @"[0-9]{3}[0-9Xx]"]; NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; if (![regexTest evaluateWithObject:value]) { return NO; } int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3; NSInteger remainder = summary % 11; NSString *checkBit = @""; NSString *checkString = @"10X98765432"; checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位 return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]; } /// 效验邮箱格式 - (BOOL)validateEmail { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:self]; } @end