【代碼筆記】驗證手機號,郵箱,車牌號是否合法,手機號車牌號
一,代碼。
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"------%d------",[self isValidateEmail:@"12345"]); NSLog(@"------%d------",[self isValidateMobile:@"12345"]); NSLog(@"---%d---------",[self isValidateCar:@"12345"]); }#pragma -mark -郵箱驗證-(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];}#pragma -mark -手機號碼驗證-(BOOL) isValidateMobile:(NSString *)mobile{ //手機號以13, 15,18開頭,八個 \d 數字字元 NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex]; // NSLog(@"phoneTest is %@",phoneTest); return [phoneTest evaluateWithObject:mobile];}#pragma -mark -車牌號驗證-(BOOL)isValidateCar:(NSString *)car{ NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$"; NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex]; NSLog(@"carTest is %@",carTest); return [carTest evaluateWithObject:car];}
二,輸出。
2015-10-22 10:00:05.694 驗證手機號,郵箱,車牌號是否合法[1459:49136] ------0------2015-10-22 10:00:05.694 驗證手機號,郵箱,車牌號是否合法[1459:49136] ------0------2015-10-22 10:00:05.695 驗證手機號,郵箱,車牌號是否合法[1459:49136] carTest is SELF MATCHES "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"2015-10-22 10:00:05.695 驗證手機號,郵箱,車牌號是否合法[1459:49136] ---0---------