angular-easy-masks

AngularJS component for input fields based on a configurable mask. Compatible with AngularJS 1.3.4+. Works together with ngModelOptions introduced in AngularJS 1.3.

Live Demo

Why?

The problem of typing data according to some predefined mask is nothing new. Solutions already exists for this, even natively in AngularJS. Unfortunately, I didn't managed to find a solution that worked the way it should.

All is needed to such a component is to:

a) Guide user through typing on an input field, inserting character separators where appropriate; and 
b) If there's an existing value, it should be presented according the mask provided.

Quite simple, it seems.

But in practice, not so. I freaked out when I became aware how some libraries approached these requirements (and with hundreds of lines of code, and dozens of concepts to learn). It looks like clumsy code is winning!! But I refuse to use that code in my projects (it's better when we don't see the code, though).

Then I decided to tackle on this problem and here I present you my approach. I also took it as a exercise of good design, so it was also pleasing to write. When I realized it was actually doing its job well, I decided to polish it and share. From the first prototype, it took a couple of hours more to have it working the AngularJS way. A handful more to make it actually releasable (bower, npm, git).

All this work is based on the following assumptions:

Features

Also, the following requirements can be achieved by means of integrating with other components:

Recognized mask wildcards

Future Work

Current version: v0.2.0

Usage

<input type="text" ng-model="ctrl.field" wt-easy-mask="999.999.999-99" />

or

<input type="text" ng-model="ctrl.field" wt-easy-mask="999.999.999-99" placeholder="Type your Identitication Number"/>

Configuration

app.config(function (easyMaskProvider) {
    easyMaskProvider.publishMask('myId', '999.999.999-99');
});

Directives

wt-easy-mask

Filters

{{data | easyMask:'99.999-9'}}
{{data | easyMask:'myId'}}

Installation

Bower

bower install angular-easy-masks --save

Application

HTML

<script type="text/javascript" src="release/angular-easy-masks.min.js"></script>

JavaScript

var app = angular.module('app', ['wt.easy']);

License

MIT