Sterling Lutes
Sterling Lutes

Reputation: 43

Checking if password field and verify password fields are not equal?

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.

Upvotes: 0

Views: 9293

Answers (3)

Dilip Tiwari
Dilip Tiwari

Reputation: 1461

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];

Upvotes: 0

Praveen-K
Praveen-K

Reputation: 3401

-(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;
}

Upvotes: 5

akashivskyy
akashivskyy

Reputation: 45210

Use isEqualToString: method for comparing strings:

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

Upvotes: 5

Related Questions