5

Im trying to create a function that reads the value of an input and triggers a series of true/false, however the code below keeps returning "passStrength is not defined." From what I can find, oninput isn't supported by Angular. How can achieve this in Angular?

Within my controller:

$scope.passStrength = function(input) {
    if (input.value.toLowerCase().indexOf(/[a-z]/) > -1) {
        $scope.lwrChar = true;
        console.log('lower ' + $scope.lwrChar);
    } else if (input.value.toUpperCase().indexOf(/[A-Z]/) > -1) {
        $scope.uprChar = true;
        console.log('upper ' + $scope.uprChar);
    } else if (input.value.indexOf() == !isNaN(n)) {
        $scope.nbrChar = true;
        console.log('number ' + $scope.nbrChar);
    } else if (input.value.length >= 8) {
        $scope.countChar = true;
        console.log('count ' + $scope.countChar);
    }
};

and in my markup:

<input id="password" oninput="passStrength()" />
1

1 Answer 1

7

To fire an event when the input changes, use ng-change. Also, you must define a ng-model.

<input ng-model="password" ng-change="passStrength(password)" />

Edit: Created a plunker demonstrating it

6
  • Thanks but then how do I refer to the model in the controller? replacing input with $scope.password doesnt do the trick. Im getting an undefined error from the function for input. Commented Apr 9, 2016 at 23:19
  • Take a look at this Plunker.
    – tpsilva
    Commented Apr 11, 2016 at 12:15
  • Thanks! I took a slightly different path but essentially the same! Commented Apr 12, 2016 at 14:18
  • Maybe it's worth mentioning that ng-change won't evaluate the expression whenever the model changes, but whenever it's changed by the user using the input field. Commented Nov 19, 2016 at 21:03
  • 2
    onchange and oninput are not the same, because onchange only fires when the field loses focus. While this answer may solve the OP's problem, it doesn't actually answer his question.
    – nshew13
    Commented Feb 4, 2017 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.