-1

Hi can any one help how i can validate Password and Confirm Password Fields in AngularJS

RameshKumar
  • 164
  • 1
  • 16
  • Please read through Stackoverflow's guide on [how to ask questions](http://stackoverflow.com/help/how-to-ask) for future questions. – muenchdo Sep 17 '15 at 09:58
  • please do some initial research before posting any question here. anyway below answer will help you. – tech-gayan Sep 17 '15 at 10:00

1 Answers1

0

there are so many ways you can achive this. one way is using directive

<input type="password" name="confirmPassword" 
    ng-model="registration.user.confirmPassword"
    required 
    compare-to="registration.user.password" />

<div ng-messages="registrationForm.confirmPassword.$error"
  ng-messages-include="messages.html"></div>

//Directive

var compareTo = function() {
  return {
    require: "ngModel",
    scope: {
        otherModelValue: "=compareTo"
    },
    link: function(scope, element, attributes, ngModel) {

        ngModel.$validators.compareTo = function(modelValue) {
            return modelValue == scope.otherModelValue;
        };

        scope.$watch("otherModelValue", function() {
            ngModel.$validate();
        });
    }
  };
};

module.directive("compareTo", compareTo);

Reference

tech-gayan
  • 1,373
  • 1
  • 10
  • 25