Regular expression input mask for angular?

Angular UI Utils' ui-mask project ( link ) is too limited.

For example, the first digit of a US phone number should be 2-9. In ngPattern it should be

 ng-pattern="/^\([2-9]\d{2}\)\d{3}-\d{4}(x\d{1,4})?$/" 

So, how can I write an input mask so that users cannot enter 0 or 1 in the first digit? Is there some better input mask we should use for Angular?

+6
source share
2 answers

I think you can find your answer here: https://github.com/angular-ui/ui-utils/issues/16

As explained in the link, you can get the mask from the scope / controller variable, check the input and change the mask as necessary, for example:

 <input type="text" ui-mask="{{mask}}" ng-keyup="onKeyUp()" ng-model="myinput"> $scope.myinput = ''; var defaultMask = '(99) 9999-9999'; $scope.mask = defaultMask; $scope.onKeyUp = function(){ if ($scope.myinput.slice(0,3) == '119') { // (11) 9 means mobile, or instead, you could use a regex $scope.mask = '(99) 99999-9999'; } else { $scope.mask = defaultMask; } }; 
+3
source

Allow specific inputs based on a regular expression pattern for AngularJS

http://alphagit.imtqy.com/ng-pattern-restrict/

0
source

Source: https://habr.com/ru/post/981892/


All Articles