Skip to content

Instantly share code, notes, and snippets.

@pranav7
Last active March 31, 2016 11:09
Show Gist options
  • Save pranav7/fcc68475e8270107dba879f69ad7a382 to your computer and use it in GitHub Desktop.
Save pranav7/fcc68475e8270107dba879f69ad7a382 to your computer and use it in GitHub Desktop.
Angular Directive for limiting number input fields to maximum characters

Angular Directive for limiting number input fields to maximum characters

do ->
  'use strict'

  LimitTo = () ->
    return {
      restrict: "A"
      link: (scope, element, attrs) ->
        limit = parseInt(attrs.limitTo)

        element.on 'keydown', (event) ->
          return if event.keyCode == 8

          if @value.length == limit
            event.preventDefault()
            return
        return
    }

  LimitTo.$inject = []

Sorry I just write coffeescript, you can always use this amazing tool js2.coffee, to get the javascript version of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment