我有一个注册视图控制器,它包含多个文本字段来注册用户。 我需要验证所有文字字段,例如非空,有效电子邮件,用户名,密码等。显示所有不同情况的警报消息。
现在我遵循以下方法:
if (condition) {
if (condition) {
if (condition) {
} else {
[alert show];
}
} else {
[alert show];
}
} else {
[alert show];
}
我知道这不是最好的方法。所以大家请建议一个合适的方法来完成这项任务。
谢谢,
答案 0 :(得分:3)
多个If else条件
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
NSString *mobileRegex = @"[0-9]{6,14}$";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex]
if (txtName.text.length == 0)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter Name" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if (txtMobile.text.length == 0)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter Mobile Number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if ([mobileTest evaluateWithObject:txtMobile.text] == NO)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter valid Mobile Number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if (txtMobile.text.length < 10)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter valid Phone Number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if (txtMobile.text.length > 10)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter valid Phone Number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if (txtEmail.text.length == 0)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter Email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else if ([emailTest evaluateWithObject:txtEmail.text] == NO)
{
[[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please Enter valid Email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
else
{
//success Code
}
答案 1 :(得分:2)
此处isAllFieldsAreValid()
将验证您可以在此处添加所有验证的所有字段。
showAlert
是一种显示错误提醒的方法。
allTrim()
是一个可以修剪空格的宏。
- (BOOL)isAllFieldsAreValid {
//here only empty string is checked you can add other if-else to validate email, phno, etc.
if ([allTrim(self.txtFname.text) isEqualToString:@""]) {
[self showAlert:@"Please enter first name."];
return false;
} else if ([allTrim(self.txtLname.text) isEqualToString:@""]) {
[self showAlert:@"Please enter last name."];
return false;
} else if ([allTrim(self.txtEmail_SignUp.text) isEqualToString:@""]) {
[self showAlert:@"Please enter email id."];
return false;
} else if ([allTrim(self.txtPassword_SignUp.text) isEqualToString:@""]) {
[self showAlert:@"Please enter password."];
return false;
}
return true;
}
你可以点击按钮调用它,然后根据真假你可以采取行动。
- (IBAction)buttonTappedInLoginView:(UIButton *)sender {
if ([self isAllFieldsAreValid]) {
// do stuff
}
}
答案 2 :(得分:0)
使用此代码,
if (firstnametf.text.length==0 || lastnametf.text.length==0 || emailtf.text.length==0 || myimageView.image == nil || commenttf.text.length==0 || [commenttf.text isEqualToString:@"Comment"])
{
[self validatetextfield];
}
else if (![emailtf.text isEqualToString:@""])
{
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:emailtf.text] == YES)
{
//All conditions are checked, you will set the function
}
else if ()
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test!" message:@"Please Enter Valid Email Address. \nex. fdsjfkd@mail.com" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
}
方法:
-(void) validatetextfield
{
if (firstnametf.text.length==0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Firstname Field Empty!" message:@"Please Enter the Valid Details" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[firstnametf becomeFirstResponder];
}
else if (lastnametf.text.length==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Lastname Field Empty!" message:@"Please Enter the Valid Details" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[lastnametf becomeFirstResponder];
}
else if (emailtf.text.length==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email Field Empty!" message:@"Please Enter the Valid Details" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[emailtf becomeFirstResponder];
}
else if(commenttf.text.length==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Comment Field Empty!" message:@"Please Enter the Valid Details" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[commenttf becomeFirstResponder];
}
else if ([commenttf.text isEqualToString:@"Comment"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Comment Field Empty!" message:@"Please Enter the Valid Details" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[commenttf becomeFirstResponder];
}
else if (myimageView.image == nil)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image not Upload!" message:@"Please Upload Image" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
更改警报条件,希望其有用