9

The ui-mask in `Angular UI Utils' project (link) is a bit 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 to prevent users from entering 0 or 1 in the first digit? Is there some better input mask we should use for Angular?

1
  • Not sure what you want. If you want to avoid entering those numbers at the beginning you can start your regex with ^[^01] Commented Apr 8, 2015 at 21:08

2 Answers 2

4

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

As it is explained in the link you can get the mask from a scope/controller variable, check the input and change the mask as needed like:

<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;
  }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, There are so many useful comments in that link. But the best one in my point of view was this one.
0

Allowing certain inputs based on a regex pattern for AngularJS

http://alphagit.github.io/ng-pattern-restrict/

2 Comments

This project is old and not maintained anymore. Could you provide an alternative?
sorry, but I didn't work with Angular for several years, not sure what to use now

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.