2012-09-04 3 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. 일반적으로 비즈니스 로직과 관련하여 비어있는 문자열과 비어있는 문자열 사이에는 차이가 없으므로 일반적으로 두 조건을 모두 확인하고 동일한 작업을 수행해야합니다.
    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과 비교해서는 안됩니다. 오히려 @""과 비교하십시오. 또한 너무 공백 문자를 트리밍 할 수 있습니다

[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
0

시도

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

사용이가있는 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

단지 텍스트 필드 작업은 문자열을 포함, 텍스트 필드는 루프에게 경고를주고 값 MSG = @를 입력 "완전히 성공을 등록"하지 – NANNAV

+0

난 정말이 의견을 얻을하지 말자을 비울 것입니다! 그리고 모든 필드에 공백을 넣으면됩니까? 이 유효성 검사는 OK라고 말하고, 모든 것은 OK이며 무엇을할까요? @ ""같은 이름? .... Woz가 전에해야한다고 말했다. [string 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 ....이 솔루션으로 확인하십시오. –