【发布时间】:2016-05-13 11:54:06
【问题描述】:
目前我正在做有关编辑文本字段验证的项目。我有验证来自编辑文本字段的输入的方法(代码如下)。
- (BOOL) validatePhoneNumber: (NSString *) number {
NSString *phoneRegex = @"[0-9]{10}";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phoneTest evaluateWithObject:number];
//[self validatePhoneNumber:yourTextField]
}
然后我有 if else 检查值。但是,我不知道如何从方法中获取值并在 if else 中进行比较。这个问题有什么建议或解决方案。我在 XCode 中使用Objective-C。
下面是我的 if else 代码。
if ([[_tempDict objectForKey:@"Contact 1"] isEqualToString:@""] && [self validatePhoneNumber:[_tempDict objectForKey:@"Contact 1"]] && ![self validatePhoneNumber:[_tempDict objectForKey:@"Contact 2"]]) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Invalid"
message:@"Please enter at least one valid contact."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
return NO;
}
【问题讨论】:
-
@beyowulf ![self validatePhoneNumber:[_tempDict objectForKey:@"Contact 2"]
标签: ios objective-c regex validation