Frank Dixon
Frank Dixon

Reputation: 41

Comparing two UITextFields

I have no idea why this isn't working and I feel it's something really very stupid.. I'm trying to compare two textfields (like a confirming the password type of thing) but it just isn't working!! Ive tried several different options and none of them work. I've tried:

if (passwordCheckField1.text == passwordCheckField2.text) {
        [self performSegueWithIdentifier:@"segue4" sender:nil];
    }

I've tried:

NSString *retrivedPasswordCheckField1 = passwordCheckField1.text;
NSString *retrivedPasswordCheckField2 = passwordCheckField2.text;
if ([retrivedPasswordCheckField1 isEqualToString:retrivedPasswordCheckField2]) {
        [self performSegueWithIdentifier:@"segue4" sender:nil];
    }

I've tried:

if ([passwordCheckField1.text isEqualToString:retrivedPasswordCheckField2.text]) {
        [self performSegueWithIdentifier:@"segue4" sender:nil];
    }

I've implement the <UITextFieldDelegate> in the .h and i've also set the delegates for both fields, (passwordCheckField1.delegate = self; passwordCheckField2.delegate = self;)

What am I missing???? Thanks for your help!

EDIT

I probably should mention that I'm using a Navigation Controller, with several child view controllers. Each contains a text field, so when the view controller about the first password comes up, the user enters it and then clicks next taking them to the confirm password view controller. All are linked to the same class files.

Upvotes: 0

Views: 1022

Answers (5)

Gaurav Singh
Gaurav Singh

Reputation: 1997

As a precautionary measure, try triming both the password strings for leading and trailing spaces before comparing like

NSString *pwd1 = [pwdTxtField1.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];

Upvotes: 0

Shehbaz Khan
Shehbaz Khan

Reputation: 1990

This should work.

NSString *password = passwordCheckField1.text;
NSString *confirmPassword = passwordCheckField2.text;

NSLog(@"password: '%@'", password);
NSLog(@"confirmPassword: '%@'", confirmPassword);

if([password isEqualToString: confirmPassword]) {
       NSLog(@"Match");

} else {
        NSLog(@"Not Match");

}

Upvotes: 4

Tapas Pal
Tapas Pal

Reputation: 7207

try this

if([[passwordCheckField1 text] isEqualToString:[passwordCheckField2 text]])
{
    [self performSegueWithIdentifier:@"segue4" sender:nil];
}
else
{
   //not matched
}

Upvotes: 0

Muhammed Ayaz
Muhammed Ayaz

Reputation: 1398

chk your values.

 if ([passwordCheckField1.text isEqualToString:passwordCheckField2.text]) {   
    //Match it's working for me 
 }

Upvotes: 1

Ankush
Ankush

Reputation: 2555

if (passwordCheckField1.text == passwordCheckField2.text) {
    [self performSegueWithIdentifier:@"segue4" sender:nil];
}

The above code doesn't compare two string values. It actually compares two pointers value.So it is not going to be useful for you.

but code

if ([retrivedPasswordCheckField1 isEqualToString:retrivedPasswordCheckField2]) { [self performSegueWithIdentifier:@"segue4" sender:nil]; }

should have compared the string values. May be string your values are not same or nil. Have you printed the passwordCheckField1.text and passwordCheckField2.text.What is the values for two strings ?

Upvotes: 1

Related Questions