-
StackOverflow 文件
-
iOS 教程
-
NSPredicate
-
使用 NSPredicate 進行表單驗證
NSString *emailRegex = @"[A-Z0-9a-z]([A-Z0-9a-z._-]{0,64})+[A-Z0-9a-z]+@[A-Z0-9a-z]+([A-Za-z0-9.-]{0,64})+([A-Z0-9a-z])+\\.[A-Za-z]{2,4}"; NSString *firstNameRegex = @"[0-9A-Za-z\"'-]{2,32}$";
NSString *firstNameRegex = @"[ 0-9A-Za-z]{2,32}$";
NSString *lastNameRegex = @"[0-9A-Za-z\"'-]{2,32}$";
NSString *mobileNumberRegEx = @"^[0-9]{10}$";
NSString *zipcodeRegEx = @"^[0-9]{5}$";
NSString *SSNRegEx = @"^\\d{3}-?\\d{2}-?\\d{4}$";
NSString *addressRegEx = @"^[ A-Za-z0-9]{2,32}$";
NSString *cityRegEx = @"^[ A-Za-z0-9]{2,25}$";
NSString *PINRegEx = @"^[0-9]{4}$";
NSString *driversLiscRegEx = @"^[0-9a-zA-Z]{5,20}$";
-(BOOL)validateEmail {
//Email address field should give an error when the email address begins with ".","-","_" .
NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return ([emailPredicate evaluateWithObject:self.text] && self.text.length <= 64 && ([self.text rangeOfString:@".."].location == NSNotFound));
}
- (BOOL)validateFirstName {
NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", firstNameRegex];
return [firstNamePredicate evaluateWithObject:self.text];
}
- (BOOL)validateLastName {
NSPredicate *lastNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", lastNameRegex];
return [lastNamePredicate evaluateWithObject:self.text];
}
- (BOOL)validateAlphaNumericMin2Max32 {
NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", firstNameRegex];
return [firstNamePredicate evaluateWithObject:self.text];
}
- (BOOL)validateMobileNumber {
NSString *strippedMobileNumber = [[[[self.text stringByReplacingOccurrencesOfString:@"(" withString:@""]
stringByReplacingOccurrencesOfString:@")" withString:@""]
stringByReplacingOccurrencesOfString:@"-" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSPredicate *mobileNumberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileNumberRegEx];
return [mobileNumberPredicate evaluateWithObject:strippedMobileNumber];
}
- (BOOL)validateZipcode {
NSPredicate *zipcodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", zipcodeRegEx];
return [zipcodePredicate evaluateWithObject:self.text];
}
- (BOOL)validateSSN {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", SSNRegEx];
return [predicate evaluateWithObject:self.text];
}
- (BOOL)validateAddress {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", addressRegEx];
return [predicate evaluateWithObject:self.text];
}
- (BOOL)validateCity {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", cityRegEx];
return [predicate evaluateWithObject:self.text];
}
- (BOOL)validatePIN {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PINRegEx];
return [predicate evaluateWithObject:self.text];
}
- (BOOL)validateDriversLiscNumber {
if([self.text length] > 20) {
return NO;
}
NSPredicate *driversLiscPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", driversLiscRegEx];
return [driversLiscPredicate evaluateWithObject:self.text];
}