我的应用程序中有4个文本域
1.username 2.Email 3.Age 4.Password
用户名为3-25个字符,仅包含字符[a-z0-9]
年龄必须在1-100之间。
密码介于4到12个字符之间,仅使用字符[a-zA-Z0-9]
如何限制具有上述要求的文本字段
请有人帮我解决这个问题。
感谢您的努力和考虑。
答案 0 :(得分:0)
您应该查看UITextFieldDelegate
协议。特别是,你想要的东西可以用textField:shouldChangeCharactersInRange:replacementString:
方法编程。
对于最小长度,您可以使用textFieldShouldEndEditing:
例如,要检查用户名的长度:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == _userNameTextField && [[textField text] length] - range.length + [string length] > 25) {
return NO;
}
// add any other check here
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (textField == _userNameTextField && [[textField text] length] < 3) {
return NO;
}
return YES;
}
答案 1 :(得分:0)
- (BOOL)validateEmail:(NSString *)emailStr {
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:emailStr];
}
然后,如果电子邮件地址错误,您可以显示警告:
- (void)checkEmailAndDisplayAlert {
if(![self validateEmail:[aTextField text]]) {
// user entered invalid email address
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
} else {
// user entered valid email address
}
}
密码:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//delete
if (string.length == 0) {
return YES;
}
if (self.regEx) {
NSMutableString* check = [NSMutableString stringWithString:theTextField.text];
[check replaceCharactersInRange:range withString:string];
NSTextCheckingResult* match = [self.regEx firstMatchInString:check options:0 range:NSMakeRange(0, [check length])];
if (match.range.length != check.length) {
return NO;
}
}
}
答案 2 :(得分:0)
对于每个文本字段,您需要进行验证 我给你举个例子
(1)对于用户名 验证:
{
if (![self isEmpty:candidate])
{
NSString *alphaNumberRegex = @"^\\s*([A-Z0-9a-z ]*)\\s*$";
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberRegex];
return [alphaNumberTest evaluateWithObject:candidate];
}
return NO;
}
(2)用于电子邮件验证 +(BOOL)isValidEmailAddress:(NSString *)候选者 {
if (![self isEmpty:candidate])
{
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:candidate];
}
return NO;
}
(3)年龄验证
Apple提供数字键盘。因此,对于年龄,您可以使用仅包含数值的键盘
(4)进行密码验证
(BOOL)isPasswordValid:(NSString *)pwd {
if([pwd length]&lt; 8 || [pwd length]&gt; 100)
return NO;
if(![self isEmpty:pwd]) { NSString alphaNumberRegex = @&#34; ^ \ s ([A-Z0-9a-z] )\ s $&#34 ;;
NSPredicate *alphaNumberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alphaNumberRegex];
return [alphaNumberTest evaluateWithObject:pwd];
} 返回NO; }