Вопрос:

Backspace key doesn't trigger ng-change / ng-keyup in angular 1.5 it deletes the last character in a textbox

javascript angularjs

721 просмотра

1 ответ

26 Репутация автора

Thank you in advance for any advice. I have been banging my head on the wall with this one for a bit. So far I have tried to use ng-change, ng-keyup, and ng-keypress.

HTML

<input type="password" ng-model="newPass" id="passwd" ng-keyup="validatePassword()"/>

Controller

$scope.validatePassword = function () {
        var onecap = false;
        var onenum = false;
        var eightchars = false;

        if ($scope.newPass.match(/^(?=.*[A-Z])/)) {
            $scope.oneCap = true;
        } else {
            $scope.oneCap = false;
        }
        if ($scope.newPass.match(/^(?=.*\d)/)) {
            $scope.oneNum = true;
        } else {
            $scope.oneNum = false;
        }
        if ($scope.newPass.match(/^[A-Za-z\d$@$!$#$^$($)$_$+%*?&]{8,}$/)) {
            $scope.eightChars = true;
        } else {
            $scope.eightChars = false;
        }

        $scope.validated = onecap && onenum && eightchars;
    };

For whatever reason, the 'delete / backspace' works and each scope variable is updated no problem, unless the delete key is deleting the last character in the textbox.

I do realize I can use a single string to test the password requirements; I have them separated to show the user what part of their password is lacking.

Автор: Jay Guyll Источник Размещён: 22.08.2016 08:56

Ответы (1)


0 плюса

26 Репутация автора

Решение

I resolved this by blocking the match of an undefined variable to a regex.

if ($scope.newPass !== undefined) {
  validatePassword();
} else {
  return false;
}
Автор: Jay Guyll Размещён: 22.08.2016 09:15
Вопросы из категории :
32x32