【问题标题】:Angular (JavaScript) Confirm password validation message?Angular(JavaScript)确认密码验证消息?
【发布时间】:2017-07-14 22:23:09
【问题描述】:

所以我有一个密码并确认密码,如果不匹配,我显然需要匹配并显示一条消息。

我的要求:

  1. 该消息应仅在确认密码(第二次输入)输入后显示。

我现在的设置是,当用户模糊确认密码(第二次输入)时,函数运行并在不匹配时抛出错误消息,并且当用户键入与密码输入(第一次输入)匹配的正确密码(使用 onkeyup)时动态隐藏)

问题: 如果用户返回并更改第一个输入,则不会显示任何消息。如果我在密码(第一次输入)上使用相同的 onblur 功能,则在我在第二个字段中输入任何内容之前显示该消息(确认密码)。我该如何解决这个问题?

 $onInit = () => {
        let self = this;

        this.siRole.getRoles().then((roleList) => {
            self.roleList = roleList.filter((r) => {return !r.hidden});
        }, (err) => {
            self.siAlertDialog.error(err.message);
        })
        this.hide = true;
    }

    passwordWatchOnblur = ()=>{
     this.hide =  this.newUser.password == this.newUser.confirmPassword ? true :false  
      }
    passwordWatchOnkeyup = ()=>{
          if(this.newUser.password == this.newUser.confirmPassword){
              this.hide=true;
          } 
    }
    <div layout layout-xs='column'>
        <md-input-container flex class="md-accent">
          <label translate="LABELS.PASSWORD"></label>
          <input  ng-model='$ctrl.newUser.password' type='password'  required/>
        </md-input-container>
        <md-input-container flex class="md-accent">
          <label translate="LABELS.CONFPASS"></label>
          <input id="confirm" ng-model='$ctrl.newUser.confirmPassword' type='password' ng-blur="$ctrl.passwordWatchOnblur()" ng-keyup="$ctrl.passwordWatchOnkeyup()" required/>
          <span ng-hide="$ctrl.hide"  class='label-error'>{{'SI-MESSAGES.PASS-NO-MATCH'|translate}}</span>
        </md-input-container>
      </div>

【问题讨论】:

标签: javascript html css angularjs


【解决方案1】:

可能的解决方案:

对密码使用相同的onkeyup函数(第一次输入)并修改passwordWatchOnkeyup,如tihs:

passwordWatchOnkeyup = () => {
    this.hide =  typeof this.newUser.confirmPassword === 'undefined' || this.newUser.confirmPassword === null || this.newUser.password == this.newUser.confirmPassword;
}

原因:如果没有confirmPassword 或者两者相等,则隐藏消息。


更新(为passwordWatchOnblur 函数添加了替代项)

... 或者你可以在 onblur on password (first input) 上使用这个 (passwordWatchOnblur) 函数

passwordWatchOnblur = () => {
    this.hide =  typeof this.newUser.confirmPassword === 'undefined' || this.newUser.confirmPassword === null || this.newUser.password == this.newUser.confirmPassword;
}

P.S.: 函数的内容是一样的。改变的是它们被调用的时间。在onblur 上调用passwordWatchOnblur 后,该消息将在用户离开输入之前显示,而不是在他/她输入密码时显示。

【讨论】:

  • 很好,这很好用。快速的问题是,是否可以等到客户输入确认密码,例如 ng-blur,然后我更改密码(第一次输入)或修复密码(第一次输入),然后显示消息(使用 keyup/dwn) ?
  • 当然,但那将是另一种选择。你想让我也用那个替代品来更新答案吗?基本上你不会在onkeyup 上调用该函数,但在模糊时,这样也许你应该按照与passwordWatchOnkeyup 几乎相同的想法修改passwordWatchOnblur
  • 如果你能请,我试着自己模仿我做的事情是不正确的
  • @AnuRajan,请查看我的回答的 UPDATED 部分。我刚刚添加了另一种选择。它们非常相似,变化的是函数被调用的时刻。如果仍有任何问题,请告诉我,我会尽力帮助您
猜你喜欢
  • 2015-10-22
  • 2013-09-18
  • 2015-04-03
  • 2023-02-23
  • 1970-01-01
  • 2019-01-07
  • 2011-08-23
  • 2017-12-06
  • 2021-07-24
相关资源
最近更新 更多