Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex input mask for angular?

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?

like image 277
Blaise Avatar asked Feb 02 '15 20:02

Blaise


1 Answers

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;
  }
};
like image 91
UserNeD Avatar answered Oct 04 '22 19:10

UserNeD