2012-09-04 119 views
1

我使用此代码来检查文本字段是否为空。第一次,它会起作用,但当我更改文本字段值时,警报不起作用。验证多个uitextfield

-(void)sendclick { 
     NSString *msg; 
     if(firstnametextfield.text==NULL) { 
      [email protected]"enter first name"; 
      NSLog(@"%@",firstnametextfield.text); 
     } 
     else if(lastnametextfield.text==NULL) { 
      [email protected]"enter last name"; 
      NSLog(@"%@",lastnametextfield.text); 
     } 

     else if(emailtextfield.text==NULL) { 
      [email protected]"enter email address"; 
      NSLog(@"%@",emailtextfield.text); 
     } 

     else if(companytextfield.text==NULL) { 
      [email protected]"enter company name"; 
      NSLog(@"%@",companytextfield.text); 
     } 

     else if(phonetextfield.text==NULL) { 
      [email protected]"enter phone numper"; 
      NSLog(@"%@",phonetextfield.text); 
     } 
    else { 
     [email protected]"register success fully"; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
     [alert show]; 
     [alert release]; 
    } 

    } 

回答

3

有一对夫妇的事情要考虑这个代码:

  1. 正如其他人所指出的那样,这是不检查一个空字符串的正确方法。
    a。首先,如果您确实想要检查未分配的字符串,则应该检查nil而不是NULL。 b。其次,字符串也可以被分配,但是没有字符,所以你应该检查它是否是空字符串。
    c。请注意,您通常希望检查这两个条件中的两个,并且执行相同的操作,因为通常情况下,就业务逻辑而言,字符串nil与空字符之间没有区别。 d。最简单的方法是简单地检查字符串的length。这是有效的,因为如果消息发送到一个nil对象,它将返回0,如果它是一个没有字符的实际字符串,它也将返回0.

    因此,类似于此的检查将工作而不是:

    if([firstnametextfield.text length] == 0) 
    
  2. 你通常做要检查文本字段的值,直接像这样的表单验证。这是因为,在某些情况下(例如当文本字段位于表格视图单元格中并且滚动屏幕时)文本字段不可用,并且您将无法访问存储在文本字段中的数据。

相反,你应该收集它是由文本字段的委托设置为您的视图控制器,并实现以下功能进入后文:

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    if (textField == firstnametextfield) { 
     // Store the first name in a property, or array, or something. 
    } 
} 

然后,当你准备好要验证输入的信息,请使用上面#1中的技术检查您存储的值,而不是实际的文本字段值本身。

+1

很容易检查文本提交,感谢您的帮助 – NANNAV

1

你不应该NULL文本字段值进行比较。相反,将其与@""进行比较。您还可以修剪空白字符太:使用

if([lastnameTextField.text isEqualToString:@""]) 
{ 
msg = @"Enter last name"; 
NSLog(@"%@",lastnametextfield.text); 
} 

[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
0

试试这个是你如何检查是否一个的NSString为空。

0

使用==或!=运算符不能比较字符串对象。所以这里是正确的:

-(void)sendclick 
{ 
    NSString *msg; 
    if([firstnametextfield.text isEqualToString:@""]) 
    { 
     [email protected]"enter first name"; 
     NSLog(@"%@",firstnametextfield.text); 
    } 
    else 
     if([lastnametextfield.text isEqualToString:@""]) 
    { 
     [email protected]"enter last name"; 
     NSLog(@"%@",lastnametextfield.text); 
    } 

    else if([emailtextfield.text isEqualToString:@""]) 
    { 
     [email protected]"enter email address"; 
     NSLog(@"%@",emailtextfield.text); 
    } 

    else if([companytextfield.text isEqualToString:@""]) 
    { 
     [email protected]"enter company name"; 
     NSLog(@"%@",companytextfield.text); 
    } 

    else if([phonetextfield.text isEqualToString:@""]) 
    { 
     [email protected]"enter phone numper"; 
     NSLog(@"%@",phonetextfield.text); 
    } 
    else 
    { 

    [email protected]"register success fully"; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
    [alert show]; 
    [alert release]; 
} 

} 

我希望能帮到你。

+0

它的工作只是文本字段包含字符串,文本字段将清空它不会进入循环警报给定值,味精= @“充分注册成功”, – NANNAV

+0

我真的dont't得到这个评论!如果我只是在所有领域的空白?这个验证会说OK,一切正常,什么?像@“”这样的名字? .... Woz说你应该这样做[字符串stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; –

1
- (BOOL)isEmptyOrNull:(NSString*)string { 
    if (string) { 
     if ([string isEqualToString:@""]) { 
      return YES; 
     } 
     return NO; 
    } 
    return YES; 
} 


-(void)sendclick { 
    NSString *msg; 
    if([self isEmptyOrNull:firstnametextfield.text]) { 
     [email protected]"enter first name"; 
     NSLog(@"%@",firstnametextfield.text); 
    } 
    ..... 
    ..... 
    ..... 
0
-(void)sendclick 
    { 
     if ([self ControlValidation]==YES) 
     { 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucess" message:@"Registration done successfully" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
     } 
    } 
    -(BOOL)ControlValidation 
    { 
     if ([self isEmpty:firstnametextfield.text ]==YES) 

     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      return NO; 
     } 
     if ([self isEmpty:lasttnametextfield.text ]==YES) 

     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 

      return NO; 
     } 
     return YES; 
    } 


    -(BOOL)isEmpty:(NSString *)str 

    { 
     if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) 
     { 
      return YES; 

     } 

     return NO; 
    } 
0
-(void)validateTextFields 
     { 
      if ([self Validation]==YES) 
      { 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucess" message:@"Registration done successfully" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
     } 
    } 
    -(BOOL)Validation 
    { 
     if ([self isEmpty:firstnametextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return NO; 
    } 
    if ([self isEmpty:lasttnametextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

     return NO; 
    } 
if ([self isEmpty:Usernametextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return NO; 
    } 
    if ([self isEmpty:phonetextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

     return NO; 
if ([self isEmpty:emailtextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"First Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     return NO; 
    } 
    if ([self isEmpty:passwordtextfield.text ]==YES) 

    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Last Name is empty" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

     return NO; 
    } 
    } 
    return YES; 
} 


-(BOOL)isEmpty:(NSString *)str 

{ 
    if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) 
    { 
     return YES; 

    } 
+0

@NANNAV ....检查它与此解决方案 –