1.验证输入都是数字:
// 判断仅输入数字: + (BOOL)isInputShouldAlphaNum: (NSString *)inputStr { NSString *regex =@"[0-9]*"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; if ([pred evaluateWithObject:inputStr]) { return YES; } return NO; }
2.验证输入手机号:
+ (BOOL) IsPhoneNumber:(NSString *)number { NSString *phoneRegex1=@"1[34578]([0-9]){9}"; NSPredicate *phoneTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex1]; return [phoneTest1 evaluateWithObject:number]; }
3.邮箱验证:
// 邮箱验证 - (BOOL)isValidateEmail:(NSString *)email { 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:email]; }
4.身份证验证:
+ (BOOL) IsIdentityCard:(NSString *)IDCardNumber { if (IDCardNumber.length <= 0) { return NO; } NSString *regex2 = @"^(\d{14}|\d{17})(\d|[xX])$"; NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2]; return [identityCardPredicate evaluateWithObject:IDCardNumber]; }
5.座机号码验证:
// 座机验证 - (BOOL)isValidateTel: (NSString *)tel { //验证输入的固话中不带 "-"符号 NSString * strNum = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)"; //验证输入的固话中带 "-"符号 //NSString * strNum = @"^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)"; NSPredicate *checktest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum]; return [checktest evaluateWithObject:tel]; }
6.银行卡验证:
+ (BOOL) IsBankCard:(NSString *)cardNumber { if(cardNumber.length==0) { return NO; } NSString *digitsOnly = @""; char c; for (int i = 0; i < cardNumber.length; i++) { c = [cardNumber characterAtIndex:i]; if (isdigit(c)) { digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c]; } } int sum = 0; int digit = 0; int addend = 0; BOOL timesTwo = false; for (NSInteger i = digitsOnly.length - 1; i >= 0; i--) { digit = [digitsOnly characterAtIndex:i] - '0'; if (timesTwo) { addend = digit * 2; if (addend > 9) { addend -= 9; } } else { addend = digit; } sum += addend; timesTwo = !timesTwo; } int modulus = sum % 10; return modulus == 0; }
暂时遇到这么多,后面继续记录更新吧!!!