2011-08-12 6 views
0

私は働いているアプリに登録フォームを持っています。あなたの電子メールにあなたの希望のパスワードと確認パスワードを入力します。 2つのパスワードフィールド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/2つのテキストのテキストを比較する –

答えて

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(![パスワード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]; 
関連する問題