0

I have a registration form on an app im working on. You enter your email then your desired password and a verify password. It needs to check if the two password fieldspasswordFieldOne and passwordFieldTwo and not equal. They need to check if they are not equal for my error process to work correctly. Here are some things I tried.

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

-

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

-

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

PS. Please dont respond telling me I can just check if they are equal, or check if they are equal and then say else. Thanks for your help.

3 Answers3

5

Use isEqualToString: method for comparing strings:

if([passwordFieldOne.text isEqualToString:passwordFieldTwo.text]) {
    // passwords are equal
}
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
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;
}
Praveen-K
  • 3,401
  • 1
  • 23
  • 31
0

You below code of comparing _passwordText and _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];
Dilip Tiwari
  • 1,441
  • 18
  • 31