0

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 -->
2
  • github.com/angular-ui/ui-mask Commented May 30, 2017 at 6:45
  • ng-keypress is all I needed. Thank you :) Commented May 30, 2017 at 6:58

2 Answers 2

3
<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.

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

3 Comments

Thank you! For some reason, this is also disabling A B C D E F (first 6 letters of the alphabet)
(e.keyCode >= 97 && e.keyCode <= 102) is causing the A B C D E F error for some reason
a = 97 b = 98 c = 99 d = 100 e = 101 f = 102 according to expandinghead.net/keycode.html. Somehow those keycodes break it. I guess there's no support for numpads.
0

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.

2 Comments

I'm no expert on this, but I tried it and I think it's only firing after hitting the submit button. Thank you anyway for your help :)
Who told that it only works with submit? You can do it on change too.

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.