password-check directive in angularjs

Posted by mpm on Stack Overflow See other posts from Stack Overflow or by mpm
Published on 2012-12-23T15:34:49Z Indexed on 2014/06/05 9:26 UTC
Read the original article Hit count: 166

Filed under:
|

I'm writing a password verify directive :

 Directives.directive("passwordVerify",function(){
    return {
        require:"ngModel",
        link: function(scope,element,attrs,ctrl){
            ctrl.$parsers.unshift(function(viewValue){
                var origin = scope.$eval(attrs["passwordVerify"]);
                if(origin!==viewValue){
                    ctrl.$setValidity("passwordVerify",false);
                    return undefined;
                }else{
                    ctrl.$setValidity("passwordVerify",true);
                    return viewValue;
                }
            });

        }
    };
});

html :

<input data-ng-model='user.password' type="password" name='password' placeholder='password' required>
<input data-ng-model='user.password_verify' type="password" name='confirm_password' placeholder='confirm password' required data-password-verify="user.password">

Given 2 password fields in a form, if both password values are equal then the field affected by the directive is valid. The issue is that it works one way (i.e. when I type a password in the password-verify field). However, when the original password field is updated, the password-verify doesn't become valid.

Any idea how I could have a "two way binding verify?"

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about angularjs