Skip to content

Instantly share code, notes, and snippets.

@edymerchk
Created January 22, 2014 21:22
Show Gist options
  • Select an option

  • Save edymerchk/8567582 to your computer and use it in GitHub Desktop.

Select an option

Save edymerchk/8567582 to your computer and use it in GitHub Desktop.
Only allow to put digits directive
.directive "onlyDigits", ->
restrict: "A"
require: "?ngModel"
link: (scope, element, attrs, ngModel) ->
return unless ngModel
reg = /^-?\d*\.?\d*$/
ngModel.$parsers.unshift (inputValue) ->
digits = inputValue.split("").filter((s) ->
not !reg.test(s)
).join("")
ngModel.$viewValue = digits
ngModel.$render()
digits
<input type="text" ng-model="rangeMin" only-digits/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment