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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
Take a look at this Plunker.
Thanks! I took a slightly different path but essentially the same!
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.
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.