However, we will gladly accept new PRs or questions.
Allowing certain inputs based on a regex pattern, preventing the user from inputting anything invalid.
This project is a sister of ng-pattern-restrict (for Angular 1.x)
It is possible that at some points you may want to restrict your user from entering certain values in your web application. Specifically, fields that conform to a very strict set of values. If you decide that it is a good idea for you to restrict what the user can even type in the input, this is the tool for you.
This works similar to a masked input, except that no mask is displayed, and the validation is done against a regular expression, which allows you to permit a complex class of values if that's what you need, or to be very specific if that is your necessity.
The logic is quite simple:
- Initialization: save regex to be used from the
pattern
attribute (given that thea-pattern-restrict
directive is present). - Wait for user input, and reach on
input
(for any value change),keyup
(for keyboard entry) orclick
(for text drag and drop, contextual copy-paste, etc). - If the input matches the regex, save it as the latest valid value. Also save the current user's input caret position on the field.
- If the input does not match the regex, restore the latest valid value and set the user input caret in the same position as it was. The user experience should be as if nothing was typed or changed.
Copy the a-pattern-restrict.ts into your project, import the component and use it away.
<input type="text" pattern="[0-9]+" a-pattern-restrict />
- Make sure to use "progressive" regular expressions. The expressions are validated against the full regular expression. This means that if your regex is
\d\d
, and your textbox is empty, a user will never be able to type anything because the first keypress will not validate and will be reverted. Following the example, the proper regular expression should be\d{0,2}
. - Your regex must validate against empty strings if you want users to be able to remove the value from the textbox.
If you still have problems, please make sure to check the Compatibility notes. There are several issues that really depend on the browsers.