Validate input in angularJS $ error

1

How can I validate so that the botón does not allow me to change the password until they are equal ?, something with $error or ng-message .

I want that when I change the password validate that if there is something in the input and that they are the same, otherwise do not do anything.

var app = angular.module('myApp', []);

app.controller('appController', function ($scope, $http) {
    $scope.changePassword = function() {
        if ($scope.newPassword == $scope.confirmNewPassword) {
            console.log("contraseña son correctas");
        }else {
            console.log("contraseña no coinciden, vuelva a intentar");
        }
    }
});
<!DOCTYPE html>
<html>
  <head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="script.js"></script>

  </head>
  <body ng-app="myApp" ng-controller="appController">
    <div class="col-sm-12 col-md-12">
      <label>Nueva contrasena:</label>
      <input type="text" ng-model="newPassword" placeholder="Nueva contrasena" class="form-control" />
    </div>
    <div class="col-sm-12 col-md-12">
      <label>Repetir Contrasena:</label>
      <input type="text" ng-model="confirmNewPassword" placeholder="Repetir contrasena" class="form-control" />
    </div>
    <div class="col-sm-12 col-md-12">
      <button type="button" ng-click="changePassword()">Cambiar contrasena</button>
    </div>
  </body>
</html>
    
asked by zerokira 22.09.2017 в 19:12
source

2 answers

1

If you want to validate that they have data, then use the .length property that gives you the total of characters of string . If both are equal to 0, then you show the error:

var app = angular.module('myApp', []);

app.controller('appController', function ($scope, $http) {


    $scope.confirmNewPassword = "";
    $scope.newPassword = "";

    $scope.changePassword = function() {
        if($scope.newPassword.length == 0 || $scope.confirmNewPassword.length == 0)
        {
           console.log("Los campos contraseñas son requeridos");
        }
        else if ($scope.newPassword == $scope.confirmNewPassword) {
            console.log("contraseña son correctas");
        }else {
            console.log("contraseña no coinciden, vuelva a intentar");
        }
    }
});
<!DOCTYPE html>
<html>
  <head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="script.js"></script>

  </head>
  <body ng-app="myApp" ng-controller="appController">
    <div class="col-sm-12 col-md-12">
      <label>Nueva contrasena:</label>
      <input type="text" ng-model="newPassword" placeholder="Nueva contrasena" class="form-control" />
    </div>
    <div class="col-sm-12 col-md-12">
      <label>Repetir Contrasena:</label>
      <input type="text" ng-model="confirmNewPassword" placeholder="Repetir contrasena" class="form-control" />
    </div>
    <div class="col-sm-12 col-md-12">
      <button type="button" ng-click="changePassword()">Cambiar contrasena</button>
    </div>
  </body>
</html>
    
answered by 25.09.2017 / 15:14
source
0

I recommend you read this guide to do exactly what you need. Here they use a directive to control the match of 2 inputs and use it with $ error . Yes, I also recommend that you do not call ngMatch as they do there. Change its name since the prefix ng could cause conflict with the angularjs directives

AngularJS Password Match form validation

    
answered by 25.09.2017 в 10:05