Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use angular ui-mask to mask a phone number with optional extension?

We have the demo here on the angular-ui page.

Wondering how we can write a mask with optional extension number.

(999) 999-9999 ext. 999 will make the extension required.

like image 253
Blaise Avatar asked Jul 15 '14 15:07

Blaise


People also ask

How to add phone number mask in Angular 6?

Let's get started with angular ngx mask example. you can easily add phone number input mast in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11 and angular 12 app. In this example, we will add material design theme and then import some dependency module of input. then we will simply install ngx-mask package for phone number mask.

What is an input mask?

Input validation is always a chore to set up. An input mask is a way to enforce the format of the user’s input in a simple way. When an input mask is applied to an input element, only input in a set format can be entered.

How to add a 11th digit to a phone number mask?

Here's a fork of the example addiing validation -- angular6-phone-mask-uytj4e.stackblitz.io. Just enter numbers until you reach 10 digits, and the mask will present 111/111-1111. Try to enter an 11th digit, you'll see the field turn red despite the additional input being hidden.

How do I enter 111/111-1111 as a mask?

Just enter numbers until you reach 10 digits, and the mask will present 111/111-1111. Try to enter an 11th digit, you'll see the field turn red despite the additional input being hidden. How can one re-trigger validation after the directive alters the value?


4 Answers

I shared this same problem with you. There is a completely undocumented optional character feature for this component. I could only find it by reading the source code.

The solution for your mask would be:

(999) 999-9999 ext. ?9?9?9

Question marks in your mask will flag the next character as optional. Trust me, it works.

Sorry for taking so long to answer.

like image 54
Pedro Affonso Avatar answered Nov 11 '22 07:11

Pedro Affonso


(999) 999-9999 ext. ?9?9?9

This will work good but if you want to clear on blur and put the mask back then you need a little more, I used like this

<input type='text'
  ng-model='customer.phone'
  ui-mask="{{phoneMask}}"
  ng-blur="removeMask()"
  ng-focus="placeMask()" />

And in my controller:

$scope.phoneMask= "(999) 999-9999 ext. ?9?9?9";
$scope.removeMask = function () {
   if ($scope.rep.phone.length === 10) {
        $scope.phoneMask= "(999) 999-9999";
    }
};
$scope.placeMask = function () {
  $scope.phoneMask= "(999) 999-9999 ext. ?9?9?9";
}

Hope it will help!

like image 29
Ali Adravi Avatar answered Nov 11 '22 06:11

Ali Adravi


Still no answer for this one after two weeks. I would assume all agreed this AngularUI is too limited in handling this situation.

I ended up using jquery.inputmask (http://github.com/RobinHerbots/jquery.inputmask).

We have been using this jQuery extension library since there is no Angular. It appears still the most powerful input mask. At least it has all functions I need.

Here is my setup.

  1. First, of course is to NuGet or download the library. The current version (3.0.55) separates functions into different js files. (date, numeric, phone...) Be sure to include all that you need.

  2. Create a directive for input mask attribute or class. Use link in this directive to use jQuery to apply input mask:

Here is the code:

(function(){
    'use strict';        
    var directiveId = 'myInputMask';
    angular.module('myApp')..directive(directiveId, function() {
        return {
            restrict: 'AC',
            link: function (scope, el, attrs) {
                el.inputmask(scope.$eval(attrs.myInputMask));
                el.on('change', function() {
                    scope.$eval(attrs.ngModel + "='" + el.val() + "'");
                    // or scope[attrs.ngModel] = el.val() if your expression doesn't contain dot.
                });
            }
        }
    });
})();    
  1. At last, apply this directive in attribute or class in our Html.

Here is the Html:

<input type="text" id="phone"
    my-input-mask="{mask: '(999)999-9999[ ext. 9999]', greedy: false}"
    required="" ng-model="employee.phone">

Of course, with jquery.inputmask, we can have much more complex input mask:

<input type="text" id="salary"
    dnr-input-mask="{'alias': 'numeric', 'groupSeparator': ',', 'autoGroup': true, 'digits': 2, 'digitsOptional': true, 'placeholder': '0'}"
    model="employee.salary">

So the conclusion: AngularUI still has a long way to go to satisfy our need. At this moment, jQuery.inputmask still thrives.

like image 45
Blaise Avatar answered Nov 11 '22 06:11

Blaise


A good solution for this problem is to use ngMask which allows you to do exactly what you want without jQuery. To do that just add an '?' after the character you want to be optional:

mask="9?9999-9999

This is great for inputs like Brazilian phone numbers, which can have both 8 or 9 characters.

like image 40
Lucas Moulin Avatar answered Nov 11 '22 05:11

Lucas Moulin