【问题标题】:Checking if password field and verify password fields are not equal?检查密码字段和验证密码字段是否不相等?
【发布时间】:2011-08-12 21:41:07
【问题描述】:

我在正在开发的应用上有一个注册表单。您输入您的电子邮件,然后输入您想要的密码和验证密码。它需要检查两个密码字段passwordFieldOnepasswordFieldTwo 是否不相等。他们需要检查它们是否不相等,我的错误过程才能正常工作。以下是我尝试过的一些事情。

    if(passwordFieldOne != passwordFieldTwo){
        //do whatever
    }

-

    if(passwordFieldOne.text != passwordFieldTwo.text){
        //do whatever
    }

-

    if((passwordFieldOne.text == passwordFieldTwo.text) == False){
        //do whatever
    }

PS。请不要回复告诉我我可以检查它们是否相等,或者检查它们是否相等然后再说。感谢您的帮助。

【问题讨论】:

标签: iphone xcode passwords registration verify


【解决方案1】:
-(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;
}

【讨论】:

  • 只是对代码进行了一点改进,您没有检查“空间”计数.. 是吗?
  • 谢谢,这正是我所需要的。
  • @Sterling Lutes 没问题 :)
【解决方案2】:

使用 isEqualToString:方法比较字符串:

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

【讨论】:

  • 当然,如果你想检查不等式,只需在语句前面放一个非 (!) 运算符:if(![passwordFieldOne.text isEqualToString:passwordFieldTwo.text])
  • 谢谢,但我想比较它们是否不相等。
  • 添加! ( 和 [ 之间的运算符
【解决方案3】:

您在下面比较 _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];

【讨论】:

    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多