我是iOS开发的新手。我想了解如何验证文本字段,如果它包含具有某些特定范围的数字,例如10表示联系号码?是否有可用的预定义功能?
谢谢。
答案 0 :(得分:1)
+ (BOOL) validatePassword:(NSString *)passWord
{
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
}
答案 1 :(得分:0)
要让textfield
只接受数字,只有10位数字才能使用此功能。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *acceptedCharsters = @"0123456789";
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 10) ? NO : YES;
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptedCharsters] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
return YES;
}