Question Update: How can I prevent all characters except for the ones specified in a char array from being typed into an input field using AngularJS (or jQuery)?
Old Question:
I have a simple <input type="text" />
field in my AngularJS application and I want the user to only be able to enter the following characters into the field:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~
I know that I can add ng-pattern="allowed"
to the <input>
and then set $scope.allowed
to some regex pattern and that will mark the input invalid if any invalid characters are entered, but I also want to prevent restricted characters from being input into the field AT ALL.
So my question is composed of two questions:
- What regex pattern do I use to restrict the character set to the one I posted above?
- How do I prevent illegal characters from being entered in the field? (e.g. if you type a lowercase letter then it won't appear in the field to begin with, similarly if you try to paste in text containing any illegal characters they will be removed immediately)