Skip to content

Instantly share code, notes, and snippets.

@petebacondarwin
Created March 27, 2012 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petebacondarwin/2215441 to your computer and use it in GitHub Desktop.
Save petebacondarwin/2215441 to your computer and use it in GitHub Desktop.
AngularJS 1.0 - Masked Input Widget
<input ng-model="contact.phone" ui-mask='"(9999) 9999 9999"' placeholder="(01234)-5678-8901" required />
Widgets = angular.module('WidgetModule', [])
Widgets.directive 'uiMask', ()->
require: 'ngModel'
scope:
uiMask: 'evaluate'
link: ($scope, el, attrs, controller)->
$(el).mask($scope.uiMask)
# Add a parser that only allows the model value through if the mask is valid
controller.$parsers.push (value)->
isValid = $(el).data('mask-isvalid')
controller.$setValidity('mask', isValid)
value = if isValid then el.mask() else null
# When the element blurs, update the viewvalue
$(el).bind 'blur', ()->
$scope.$apply ()->
controller.$setViewValue(el.mask())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment