Share the post "Create Directive To Match Password In Angular JS"
Here is my code in Angular JS to check if two password fields match.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
module.directive('passwordMatch', [function () { return { restrict: 'A', require: 'ngModel', link: function (scope, elem, attrs, controller) { var checker = function () { var e1 = scope.$eval(attrs.ngModel); var e2 = scope.$eval(attrs.passwordMatch); return (!e1 && !e2) || e1 == e2; }; scope.$watch(checker, function(n) { controller.$setValidity('passwordMatch', n); }); } }; }]); |
And the HTML tags look like this:
|
1 2 3 4 5 6 |
<span style="color:red" ng-show="myform.password.$error.passwordMatch">Passwords do not match!<br/></span> <input type="password" name="password" ng-model="myform.NewPassword" required="true" password-match="myform.RetypePassword" /> <input type="password" name="retypePassword" ng-model="myform.RetypePassword" required="true" /> |
Also, you may want to add some CSS styling to your password fields to highlight its border to red if the value is not valid. Just add this CSS code:
|
1 2 3 |
input.ng-dirty.ng-invalid { border-color: red; } |