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

Blaise

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.

Blaise

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to mask phone number input in Kendo UI Grid Column

From Dev

ui-mask and ngpattern not working for phone number

From Dev

How to mask a string that contains phone number in Oracle?

From Dev

What is the use of angular ui-mask?

From Dev

What is the use of angular ui-mask?

From Dev

Angular UI Mask Validation

From Dev

phone number format mask in ios

From Dev

phone number format mask in ios

From Dev

Stack with Input Mask Phone number

From Dev

Phone mask directive in Angular 8

From Dev

How to use mask in angular-xeditable?

From Dev

How to validate a null value in a maskedtextbox phone number input mask

From Dev

Mask Phone number with brackets and spaces Java

From Dev

Regexp for get phone number mask from string

From Dev

angular-ui-mask doesn't mask as expected in the text box

From Dev

angular-ui-mask doesn't mask as expected in the text box

From Dev

How to use an image as an svg mask

From Dev

Make MaskedEditExtender Mask optional

From Dev

How do I format a phone number, zipcode, etc., without displaying a mask?

From Dev

html Phone mask

From Dev

Angular UI Bootstrap date-picker Combined With UI.Mask

From Dev

Use image from ContentPresenter as opacity mask in Windows Phone style

From Dev

How to subclass CALayer for use as another CALayer mask?

From Dev

How to use mask for Mayavi's grid plots?

From Dev

How to use QLineEdit mask with Russian letters?

From Dev

How to use a logical image as a mask for the original image?

From Dev

How and Where to use Bit Mask in java

From Dev

how to use mask to remove the background in python

From Dev

How to use mask for Mayavi's grid plots?

Related Related

HotTag

Archive