1

I´m trying to make a registration form. There are two UITextField to write the Password and to confirm it. When i write exactly the same password, it always shows: "Passwords do not match ".

-(IBAction)signUp:(id)sender{

   @try {

    if([[name text] isEqualToString:@""] || [[mail text] isEqualToString:@""] || [[phone text] isEqualToString:@""] || [[password text] isEqualToString:@""] || [[rpassword text] isEqualToString:@""] ) {
    [self alertStatus:@"Please check every field" :@"¡Alert!"];

    }else if (password.text!=rpassword.text){
    [self alertStatus:@"Passwords do not match " :@"¡Please Check!"];
    }else {

    NSString *strURL = [NSString stringWithFormat:@"http://miwebsite.com/signup.php?var1=%@&var2=%@&var3=%@&var4=%@", name.text, mail.text, phone.text, password.text];

    NSURL *url2 = [NSURL URLWithString:strURL];
    NSLog(@"url: %@", url2);
    NSError *error;
    NSData *dataURL = [NSData dataWithContentsOfURL:url2 options:0 error:&error];

     if (dataURL == nil) {
     NSLog(@"error: %@", error);
     }
     else {
    NSLog(@"dataURL: %@", dataURL);
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    // THE REST OF THE CODE GOES HERE...
        }
    }
    } @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Sorry." :@"Failed"];
  }
}

I used the debugger. password.text and rpassword.text are exactly the same word. ¿Why my code goes inside that sentence?

Thanks in advanced.

J. Yepes
  • 13
  • 3

1 Answers1

5

== in objective C is a pointer comparison instead of the content comparison. The text NSString object have 2 different memory address or pointers associated to them and although the content is same the memory they point is different and hence the statement inside this

else if (password.text!=rpassword.text){
[self alertStatus:@"Passwords do not match " :@"¡Please Check!"];
}

is executed. The pointers are different.

What you need is string comparison by doing so:

else if (![password.text.isEqualToString:rpassword.text]){
[self alertStatus:@"Passwords do not match " :@"¡Please Check!"];
}

For more explanation please check this link: Understanding NSString comparison

Community
  • 1
  • 1
kandelvijaya
  • 1,545
  • 12
  • 20