简体   繁体   English

Angular(JavaScript)确认密码验证消息?

[英]Angular (JavaScript) Confirm password validation message?

so i have a password and confirm password and I obviously need to match and show a message if not match. 所以我有一个密码并确认密码,如果不匹配,我显然需要匹配并显示一条消息。

my requirements: 我的要求:

  1. the message should only show after the confirm password (second input)input. 仅在输入确认密码(第二次输入)后才显示该消息。

my setup now is, when user blur out confirm password(second input) the function runs and throw err message if no match and also is dynamically hidden when user type the correct password (used onkeyup) that matches password input (first input) 我现在的设置是,当用户脱口确认密码(第二次输入)时,该功能运行并在不匹配时抛出err消息,并且当用户键入与密码输入(第一次输入)相匹配的正确密码(使用的onkeyup)时,该功能也被动态隐藏。

problem: if the user go back and change the first input no message is shown. 问题:如果用户返回并更改第一个输入,则不会显示任何消息。 if I use the same onblur function on password (first input), then the message shows before I input anything in the second field (confirm password). 如果我在密码(第一次输入)上使用了相同的onblur函数,则在我在第二个字段中输入任何内容(确认密码)之前,将显示该消息。 how do i fix this? 我该如何解决?

  $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> 

Possible solution: 可能的解决方案:

Use the same onkeyup function on password (first input) and modify passwordWatchOnkeyup like tihs: 对密码(第一次输入)使用相同的onkeyup函数,并像tihs一样修改passwordWatchOnkeyup

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

Why: If there is no confirmPassword or they both are equal, then hide message. 原因:如果没有confirmPassword或两者相等,则隐藏消息。


UPDATE (added alternative for passwordWatchOnblur function) 更新 (添加了passwordWatchOnblur函数的替代方法)

... or you can use this ( passwordWatchOnblur ) function on the onblur on password (first input) ...或者您可以在onblur on password(第一次输入)上使用此( passwordWatchOnblur )函数

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

PS: The content of the functions are the same. PS:功能内容相同。 What changes is the time where they are called. 变化的是调用它们的时间。 With the passwordWatchOnblur being called on the onblur the message will not be shown until the user has left the input, and not while he/she is typing the password. onblur上调用passwordWatchOnblur ,直到用户离开输入,并且在输入密码时才显示该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM