xcode 检查密码字段和验证密码字段是否不相等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7046616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if password field and verify password fields are not equal?
提问by Sterling Lutes
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 fieldspasswordFieldOneand passwordFieldTwoand not equal. They need to check if they are not equal for my error process to work correctly. Here are some things I tried.
我在我正在开发的应用程序上有一个注册表单。您输入您的电子邮件,然后输入您想要的密码和验证密码。它需要检查两个密码字段是否passwordFieldOne和passwordFieldTwo和不相等。他们需要检查他们是否不等于我的错误过程才能正常工作。这是我尝试过的一些事情。
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.
附注。请不要回复告诉我我可以检查它们是否相等,或者检查它们是否相等然后说其他。谢谢你的帮助。
回答by akashivskyy
Use isEqualToString:method for comparing strings:
isEqualToString:比较字符串的使用方法:
if([passwordFieldOne.text isEqualToString:passwordFieldTwo.text]) {
// passwords are equal
}
回答by Praveen-K
-(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;
}
回答by Dilip Tiwari
You below code of comparing _passwordText and _confirmPasswordText
你下面比较 _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];

