2012-09-04 97 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 ....檢查它與此解決方案 –