   # Remove the trailing space
   if /\d(\s|\/)+$/.test(value)
     e.preventDefault()
-    $target.val(value.replace(/\d(\s|\/)*$/, ''))
+    setTimeout -> $target.val(value.replace(/\d(\s|\/)*$/, ''))
   else if /\s\/\s?\d?$/.test(value)
     e.preventDefault()
-    $target.val(value.replace(/\s\/\s?\d?$/, ''))
+    setTimeout -> $target.val(value.replace(/\s\/\s?\d?$/, ''))
 
 #  Restrictions
 

      luhn: true
  }
]

# Limited feature detection of input event support.
# It expose us to false negative for Firefox >= 4, but it won't lead to any feature degradation, so it's not a problem.
oninput = 'keypress'
oninput = 'input' if 'oninput' of document.createElement('input')

cardFromNumber = (num) ->
  num = (num + '').replace(/\D/g, '')

  # Remove the trailing space
  if /\d\s$/.test(value)
    e.preventDefault()
    setTimeout -> $target.val(value.replace(/\d\s$/, ''))
  else if /\s\d?$/.test(value)
    e.preventDefault()
    setTimeout -> $target.val(value.replace(/\s\d?$/, ''))

# Format Expiry

reFormatExpiry = (e) ->
  setTimeout ->
    $target = $(e.currentTarget)
    value   = $target.val()
    value   = $.payment.formatExpiry(value)
    $target.val(value)

formatExpiry = (e) ->
  # Only format if input is a number
  digit = String.fromCharCode(e.which)
  return unless /^\d+$/.test(digit)

  $target = $(e.currentTarget)
  val     = $target.val() + digit

  if /^\d$/.test(val) and val not in ['0', '1']
    e.preventDefault()
    setTimeout -> $target.val("0#{val} / ")

  else if /^\d\d$/.test(val)
    e.preventDefault()
    setTimeout -> $target.val("#{val} / ")

formatForwardExpiry = (e) ->
  digit = String.fromCharCode(e.which)
  return unless /^\d+$/.test(digit)

  $target = $(e.currentTarget)
  val     = $target.val()

  if /^\d\d$/.test(val)
    $target.val("#{val} / ")

formatForwardSlash = (e) ->
  slash = String.fromCharCode(e.which)
