1

我编写了一个简单的应用程序来验证用户输入(无论是 NULL 还是超过定义的长度)。当验证失败时,它应该返回验证错误消息,否则,重定向到另一个页面。

但是,该应用程序仅返回所有场景的第一个条件(用户名为空)的消息。(如用户名填写,密码为空等)

.m 文件:

- (IBAction)doLogin {

 if(uname.text==NULL) {
     UIAlertView *err1 = [[UIAlertView alloc]
                                     initWithTitle:@"Required field!" message:@"Username is empty." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [err1 show];
 NSLog(@"%@",uname.text);         
 }

 else if(passw.text==NULL) {
     UIAlertView *err2 = [[UIAlertView alloc]
                               initWithTitle:@"Required field!" message:@"Password is empty." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [err2 show];
 NSLog(@"%@",passw.text);
 }

 else if (uname.text.length < 6)
 {
     UIAlertView *err3 = [[UIAlertView alloc]
                               initWithTitle:@"Invalid!" message:@"Enter a username longer than 6 chars." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [err3 show];
 NSLog(@"%@",uname.text);
 }

 else if (uname.text.length < 8)
 {
     UIAlertView *err4 = [[UIAlertView alloc]
                               initWithTitle:@"Invalid!" message:@"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [err4 show];
 NSLog(@"%@",uname.text);
 }
 else {
     /*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Thank you" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"OK", nil];
     [alert show];*/
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:@"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];

}
4

3 回答 3

2

karthika 的替代方案(但使用类似的结构),这将在单个消息中提供有关整个表单的反馈。也许对用户更友好一点,并且肯定会减少负面的用户交互。

-(BOOL)isFormDataValid{

    NSMutableArray *errorMessages = [[NSMutableArray alloc] init];

    if([self.emailTextField.text isEqualToString:@""])
    {
        [errorMessages addObject:NSLocalizedString(@"Please enter email",nil)];
    }
    if([self.passwordTextField.text isEqualToString:@""])
    {
        [errorMessages addObject:NSLocalizedString(@"Please enter password",nil)];
    }
    if ([errorMessages count]) {
        NSString * msgs = [errorMessages componentsJoinedByString:@"\n"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Whoops!",nil) message:msgs delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return NO;
    } else {
        return YES;
    }
}
于 2014-02-07T11:02:47.323 回答
1
-(BOOL)isFormDataValid{

    NSString *errorMessage = nil;
    UITextField *errorField;

    if([nameTextField.text isEqualToString:@""])
    {
        errorMessage = @"Please enter username";
        errorField = nameTextField;
    }
    else if([[nameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
    {
        errorMessage = @"white spaces not allowed";
        errorField = nameTextField;
    }
    else if([passwordTextField.text isEqualToString:@""])
    {
        errorMessage = @"Please enter password";
        errorField = passwordTextField;
    }
    else if([[passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
    {
        errorMessage = @"white spaces not allowed";
        errorField = passwordTextField;
    }
if (errorMessage) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed!" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [errorField becomeFirstResponder];
        return NO;
    }else{
        return YES;
    }

}
于 2013-09-24T08:21:39.500 回答
0

除了你这样做的事实

else if (uname.text.length < 8)
{
    UIAlertView *err4 = [[UIAlertView alloc]
                           initWithTitle:@"Invalid!" message:@"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [err4 show];
    NSLog(@"%@",uname.text);
}

代替

else if (passw.text.length < 8)
{
    UIAlertView *err4 = [[UIAlertView alloc]
                           initWithTitle:@"Invalid!" message:@"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [err4 show];
    NSLog(@"%@",passw.text);
}

您的代码应该可以正常工作。

另外,请记住,文本字段的文本不会为零,它只是一个空字符串 ( lenght == 0),除非您明确将其设置为nil.

于 2013-09-24T08:45:54.443 回答