我正在使用的应用程序上有一个注册表单。您输入电子邮件,然后输入所需的密码和验证密码。它需要检查两个密码字段passwordFieldOne
和passwordFieldTwo
是否相等。他们需要检查它们是否与我的错误过程不相等才能正常工作。以下是我尝试的一些事情。
if(passwordFieldOne != passwordFieldTwo){
//do whatever
}
-
if(passwordFieldOne.text != passwordFieldTwo.text){
//do whatever
}
-
if((passwordFieldOne.text == passwordFieldTwo.text) == False){
//do whatever
}
PS。请不要回应告诉我,我可以检查它们是否相等,或检查它们是否相等然后再说。谢谢你的帮助。
答案 0 :(得分:5)
使用 isEqualToString:
方法比较字符串:
if([passwordFieldOne.text isEqualToString:passwordFieldTwo.text]) {
// passwords are equal
}
答案 1 :(得分: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;
}
答案 2 :(得分: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];