gpt4 book ai didi

ios - 通过if-else条件的iOS表单验证错误无法正常工作

转载 作者:行者123 更新时间:2023-12-01 19:07:07 24 4
gpt4 key购买 nike

我编写了一个简单的应用程序,用于验证用户输入(是否为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];

}

最佳答案

-(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;
}

}

关于ios - 通过if-else条件的iOS表单验证错误无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18976008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com