                 val
 
             elem.on 'keyup', (event) ->
+
               return if invalidInput(elem, event, options)
-
-              if event.which == 8 # backspace is pressed
-                if elem.val().length == 4 || elem.val().length == 8 # a new comma is added
-                  newCursorPosition = elem[0].selectionStart - 1
-                else
-                  newCursorPosition = elem[0].selectionStart
-              else
-                if elem.val().length == 4 || elem.val().length == 8 # a new comma is added
-                  newCursorPosition = elem[0].selectionStart + 1
-                else if elem[0].selectionStart < elem.val().length # cursor is adding numbers, but is not at the end of the input field
-                  newCursorPosition = elem[0].selectionStart
-                else # cursor is adding numbers at the end of the input field
-                  newCursorPosition = elem[0].selectionStart + 1
-
-              elem.val resetCommas(elem.val()) # reformat the number in place
-              elem[0].setSelectionRange(newCursorPosition, newCursorPosition) # set the new cursor position
 
               # not updating unless value is longer than 4 digits including commas
               # still some issues moving the comma after input/delete

        link: (scope, elem, attrs, ngModelCtrl) ->
            options = getOptions scope, attrs
            isValid = makeIsValid options

            ngModelCtrl.$parsers.unshift (viewVal) ->
                noCommasVal = viewVal.replace /,/g, ''


            if options.preventInvalidInput == true
              elem.on 'keypress', (e) ->
                e.preventDefault()
                e.stopPropagation()

                return if invalidInput(elem, event, options)

                if event.which >= 48 and event.which <= 57 # insert number character
                  insertCharacter(elem, event)

                # if a key has been pressed and a reformat is required, do it
                if event.which == 8 # backspace is pressed
                  if elem.val().length == 4 || elem.val().length == 8 # a new comma is added
                    newCursorPosition = elem[0].selectionStart - 1
                  else
                    newCursorPosition = elem[0].selectionStart
                else
                  if elem.val().length == 4 || elem.val().length == 8 # a new comma is added
                    newCursorPosition = elem[0].selectionStart + 1
                  else if elem[0].selectionStart < elem.val().length # cursor is adding numbers, but is not at the end of the input field
                    newCursorPosition = elem[0].selectionStart
                  else # cursor is adding numbers at the end of the input field
                    newCursorPosition = elem[0].selectionStart + 1

                elem.val resetCommas(elem.val()) # reformat the number in place
                elem[0].setSelectionRange(newCursorPosition, newCursorPosition) # set the new cursor position

    }
]
