Tyler L
Tyler L

Reputation: 845

Angular Input Mask to only disable numbers

I'm trying to create an input mask.

I want delete and arrow key functionality plus all the other keys except 1 2 3 4 5 6. I'm using 1, 2, 3, 4, 5, 6 as hotkeys for a scheduling app. Is it possible in angular to create a mask that just disables the numbers one through six?

<input pattern="regex"> <!-- not working because it only fires after submit -->

Upvotes: 0

Views: 1298

Answers (2)

Sai
Sai

Reputation: 2658

You can use angular ng-pattern to achieve this:

<input type="text" ng-model="check" name="check" ng-pattern="/^[^1-6.]*$/">
<small class="error" ng-show="exampleForm.check.$error.pattern">Invalid pattern.</small>

Dynamically checks for invalid key enter not on submit.

Upvotes: 0

Ram_T
Ram_T

Reputation: 8484

<input type="text" ng-keypress="keyPressed($event)">

In controller

function MyCtrl($scope){
    $scope.keyPressed = function(e) {
        if (e.keyCode >= 49 && e.keyCode <= 54) { 
             event.preventDefault();
        } else {
             // do your logic
        }
    }
}

You can do with ng-pattern attribute but it allows the character and do the validation but your requirement is to disable characters.

Upvotes: 3

Related Questions