在IOS7中对文本字段进行更多验证

时间:2014-03-17 13:55:26

标签: ios objective-c validation

嗨我正在验证多个文本字段,它不能正常工作,例如,如果有4个字段意味着它的验证工作在3个字段上它不工作在第4个我尝试使用其他如果在目标c中我们如何正常使用c和其他语言,但如果没有其他语言,请告诉我如何解决这个问题。

       - (IBAction)reg:(id)sender {
     if ([name.text length] > 25 && [city.text length] > 25) {
         UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"pls enter less then 25 character in name and city" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [alert1 show];
         [alert1 release];     
      }

    if ([self validateEmail:[email text]] != 1) {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Pls enter valid email id" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];
      }

    if ([self phonevalidate:[phone text]] != 1) {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Pls enter only 10 numbers" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];
       }

   if ([name.text length] < 1 && [city.text length] < 1) {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"pls fill the empty field" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];    
      }

  else {
         pollpoliticalViewController *pollVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PollPoliticalVCID"];

    //pollpoliticalViewController *vc2 = [[pollpoliticalViewController alloc] init];
        [self presentViewController:pollVC animated:YES completion:nil];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Thanks For The Registration" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
      }
     }

在上面的代码中,第一个验证不起作用请告诉我是否有另一种方法来验证多个文本字段,或者请告诉我在上面的代码中我做错了。

感谢。

2 个答案:

答案 0 :(得分:2)

...其他如果不可用......

目标C中提供了

else if

if () {
} else if () {
} else if () {
} else {
}

http://en.wikipedia.org/wiki/Objective-C

  

Objective-C是C之上的薄层,而且是严格的   C的超集;可以用。编译任何C程序   Objective-C编译器,并在其中自由包含C代码   Objective-C类。[7]

     

Objective-C从Smalltalk派生其对象语法。全部   非面向对象操作的语法(包括原语   变量,预处理,表达式,函数声明和   函数调用)与C的函数调用相同,而语法为   面向对象的功能是Smalltalk风格的一种实现   消息传递。

答案 1 :(得分:0)

使用else if语法

- (IBAction)reg:(id)sender {
    if ([name.text length] > 25 && [city.text length] > 25) {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"pls enter less then 25 character in name and city" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];     
    } else if ([self validateEmail:[email text]] != 1) {
        // Show Alert
    } else if ([self phonevalidate:[phone text]] != 1) {
        // Show Alert
    } else if ([name.text length] < 1 && [city.text length] < 1) {
        // Show Alert
    } else {
        // Everything passed
        pollpoliticalViewController *pollVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PollPoliticalVCID"];
        [self presentViewController:pollVC animated:YES completion:nil];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Thanks For The Registration" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

或者,像这样添加一些回报:

- (IBAction)reg:(id)sender {
    if ([name.text length] > 25 && [city.text length] > 25) {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Message" message:@"pls enter less then 25 character in name and city" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert1 show];
        [alert1 release];
        return;    
    if ([self validateEmail:[email text]] != 1) {
        // Show Alert
        return;
    }
    if ([self phonevalidate:[phone text]] != 1) {
        // Show Alert
        return;
    }
    if ([name.text length] < 1 && [city.text length] < 1) {
        // Show Alert
        return;
    }
    // Everything passed
    pollpoliticalViewController *pollVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PollPoliticalVCID"];
    [self presentViewController:pollVC animated:YES completion:nil];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Thanks For The Registration" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
    [alert release];
}
相关问题