I am building my own 'Confirm Password' directive in Angular.js. I got my inspiration from this pen.
My directive:
directives.directive('pwCheck', function(){
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl){
var pw1 = attrs.pwCheck;
var pw2 = attrs.ngModel;
scope.$watchGroup([pw1,pw2],function(value){
console.log(value);
ctrl.$setValidity('pwmatch', value[0] === value[1]);
});
}
}
});
My form:
<input type="password" class="form-control" name="newpassword" placeholder="Type your new password" ng-model="newpassword"
ng-pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/"
ng-minlength="8" ng-maxlength="20" ng-required="" />
and:
<input type="password" class="form-control" name="renewpassword" id="renwewpassword" placeholder="Type your new password again"
ng-model="renewpassword" ng-required="" pw-check="newpassword" />
However, watchGroup does not seem to catch changes (keystrokes) in "newpassword".
The console.log() in the directive will print undefined for value[0], while it prints the correct input for value[1].
I can't figure out where the problem is. Any clue?