0

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)

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). 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>
jsPlayer
  • 1,195
  • 7
  • 32
  • 56
  • you can try this directive: [password-check directive in angularjs](https://stackoverflow.com/questions/14012239/password-check-directive-in-angularjs) – Fabio Carballo Jul 14 '17 at 22:31

1 Answers1

1

Possible solution:

Use the same onkeyup function on password (first input) and modify passwordWatchOnkeyup like tihs:

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.


UPDATE (added alternative for passwordWatchOnblur function)

... or you can use this (passwordWatchOnblur) function on the onblur on password (first input)

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

P.S.: The content of the functions are the same. 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.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • nice, this works great. quick question is, is it possible to wait till customer input the confirm password , like ng-blur then as soon i change the password(first input) and or fix the password (first input), then show message( use keyup/dwn)? – jsPlayer Jul 14 '17 at 22:57
  • Sure, but that would be another alternative. would you like me to update the answer with that alternative too? Basically you wouldn't call the function on `onkeyup`, but on blur out, this way maybe you should modify the `passwordWatchOnblur` following pretty much the same idea as I did with `passwordWatchOnkeyup` – lealceldeiro Jul 14 '17 at 23:01
  • sure if you can please, i try to mimic it myself something i am doing is not correct – jsPlayer Jul 15 '17 at 03:38
  • @AnuRajan, please checkout the **UPDATED** section of my answer. I just added the other alternative. They are very similar, what changes is the moment where the function is called. If any issue persists, please let me know and I will try to help you – lealceldeiro Jul 17 '17 at 13:54