Skip to content

Instantly share code, notes, and snippets.

@douglascorrea
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascorrea/d7e08f69fcbe16742da8 to your computer and use it in GitHub Desktop.
Save douglascorrea/d7e08f69fcbe16742da8 to your computer and use it in GitHub Desktop.
Password Confirmation AngularJS directive
/*
How to use:
on inputs:
<input type="password" id="pw2" name="pw2"
ng-model="pw2" ng-required="" password-confirm="pw1" />
get errors with:
"myForm.pw2.$error.passwordmatch"
*/
angular.module('myApp')
.directive('passwordConfirm', [function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
var me = attrs.ngModel;
var matchTo = attrs.passwordConfirm;
scope.$watch('[me, matchTo]', function(value){
ctrl.$setValidity('passwordmatch', scope.$eval(me) === scope.$eval(matchTo) );
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment