Angular form error if two fields do not match

Is there a way to invalidate a form if the two inputs do not match (e.g. passwords) in Angular? Similarly form.password.$error.required?

+4
source share
2 answers
 pwd1:<input type="password" ng-model="pwd1" required /><br />
 pwd2:<input type="password" ng-model="pwd" required /><br />

      <div ng-show="pwd1 && pwd">Invalid:
        <span ng-show="pwd1!==pwd">Wrong</span>
        <span ng-show="pwd1===pwd">Correct</span>
      </div>

It just checks to see if both passwords match. Angular Form Validation

Also check out this Angular Ui , which has a password matching directive

+5
source

You can use the Angular user interface, it has a directive ui-validate:

<input name="password" required ng-model="password">
<input name="confirm_password" ui-validate=" '$value==password' " ui-validate-watch=" 'password' ">

Or you can create your own directive for this

myApp.directive('matchPassword', function () {
    return {
        require: 'ngModel',
        restrict: 'A',
        scope: {
            matchPassword: '='
        },
        link: function (scope, elem, attrs, ctrl) {
            scope.$watch(function () {
                return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || scope.matchSenha === ctrl.$modelValue;
            }, function (currentValue) {
                ctrl.$setValidity('matchPassword', currentValue);
            });
        }
    };
});

and use it like this:

<input required name="passwordConfirm" match-password="model.Password" />
+3
source

Source: https://habr.com/ru/post/1545106/


All Articles