2011-08-12 85 views
0

我有一個即時通訊應用程序的註冊表單。你輸入你的電子郵件,然後輸入你想要的密碼和驗證密碼。它需要檢查兩個密碼字段passwordFieldOnepasswordFieldTwo是否相等。他們需要檢查它們是否不相同,以使我的錯誤過程正常工作。這是我嘗試過的一些事情。檢查密碼字段和驗證密碼字段是否不相等?

if(passwordFieldOne != passwordFieldTwo){ 
     //do whatever 
    } 

-

if(passwordFieldOne.text != passwordFieldTwo.text){ 
     //do whatever 
    } 

-

if((passwordFieldOne.text == passwordFieldTwo.text) == False){ 
     //do whatever 
    } 

PS。請不要回復告訴我,我可以檢查他們是否平等,或檢查他們是否平等,然後說別的。謝謝你的幫助。

+0

這已經回答了:http://stackoverflow.com/questions/6905751/compare-the-text-of-two-text-fields –

回答

5
-(BOOL)isPasswordMatch:(NSString *)pwd withConfirmPwd:(NSString *)cnfPwd { 
//asume pwd and cnfPwd has not whitespace 
    if([pwd length]>0 && [cnfPwd length]>0){ 
     if([pwd isEqualToString:cnfPwd]){ 
      NSLog(@"Hurray! Password matches "); 
      return TRUE; 
     }else{ 
      NSLog(@"Oops! Password does not matches"); 
      return FALSE; 
     } 
    }else{ 
     NSLog(@"Password field can not be empty "); 
     return FALSE; 
    } 
return FALSE; 
} 
+0

]只是代碼的小改進,你沒有檢查'空間'數..你呢? –

+0

謝謝你正是我需要的。 –

+0

@Sterling Lutes沒問題:) –

5

使用isEqualToString:方法比較字符串:

if([passwordFieldOne.text isEqualToString:passwordFieldTwo.text]) { 
    // passwords are equal 
} 
+0

當然,如果你想檢查不等式,只需在語句前加一個not('!')運算符:if(![ passwordF ieldOne.text isEqualToString:passwordFieldTwo.text])' – mopsled

+0

謝謝,但我想比較它們是否不相等。 –

+0

添加!運算符之間(和[ – akashivskyy

0

您比較之下和_passwordText的_confirmPasswordText代碼

if ([_passwordText.text isEqual:_confirmPasswordText.text]) 
     { 
      NSLog(@"Password =%@ , ConfirmPassword = %@ is equal ",_passwordText.text,_confirmPasswordText.text); 
     } 
     else { 
      UIAlertController *alertConnection= [UIAlertController 
               alertControllerWithTitle:@"" 
               message:@"Password do not match! " 
               preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction* okBtnConnection= [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                    handler:^(UIAlertAction * action){ 
                     [self.view endEditing:YES]; 
                    } 
              ]; 
      [alertConnection addAction:okBtnConnection]; 
      [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]]; 
      [self presentViewController:alertConnection animated:YES completion:nil];