Skip to content

Instantly share code, notes, and snippets.

@SafeerH
Created February 22, 2016 17:08
Show Gist options
  • Save SafeerH/3e48c58529bdd43ea0f2 to your computer and use it in GitHub Desktop.
Save SafeerH/3e48c58529bdd43ea0f2 to your computer and use it in GitHub Desktop.
`samePassword` is a directive with the purpose to validate a password input based on the value of another input. When both input values are the same the inputs will be set to valid.
(function() {
'use strict';
angular
.module('triangular.directives')
.directive('triSamePassword', samePassword);
/* @ngInject */
function samePassword() {
// Usage:
//
// ```html
// <form name="signup">
// <input name="password" type="password" ng-model="user.password" same-password="signup.confirm" />
// <input name="confirm" type="password" ng-model="user.confirm" same-password="signup.confirm" />
// </form>
// ```
// Creates:
//
// `samePassword` is a directive with the purpose to validate a password input based on the value of another input.
// When both input values are the same the inputs will be set to valid
var directive = {
restrict: 'A',
require: 'ngModel',
link: link,
scope: {
triSamePassword: '='
}
};
return directive;
function link(scope, element, attrs, ngModel) {
ngModel.$viewChangeListeners.push(function() {
ngModel.$setValidity('samePassword', scope.triSamePassword.$modelValue === ngModel.$modelValue);
scope.triSamePassword.$setValidity('samePassword', scope.triSamePassword.$modelValue === ngModel.$modelValue);
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment