Skip to content

Commit

Permalink
Improved credit card number and expiry input formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvillar committed May 10, 2013
1 parent a4c4e68 commit 9248498
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/jquery.payment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/jquery.payment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@ formatBackCardNumber = (e) ->

return if e.meta

# Return unless backspacing
return unless e.which is 8

# Return if focus isn't at the end of the text
return if $target.prop('selectionStart')? and
$target.prop('selectionStart') isnt value.length

# If we're backspacing, remove the trailing space
if e.which is 8 and /\s\d?$/.test(value)
# Remove the trailing space
if /\d\s$/.test(value)
e.preventDefault()
$target.val(value.replace(/\d\s$/, ''))
else if /\s\d?$/.test(value)
e.preventDefault()
$target.val(value.replace(/\s\d?$/, ''))

Expand Down Expand Up @@ -227,7 +233,10 @@ formatBackExpiry = (e) ->
$target.prop('selectionStart') isnt value.length

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

Expand Down Expand Up @@ -379,7 +388,7 @@ $.payment.validateCardExpiry = (month, year) =>
return false unless /^\d+$/.test(month)
return false unless /^\d+$/.test(year)
return false unless parseInt(month, 10) <= 12

if year.length is 2
prefix = (new Date).getFullYear()
prefix = prefix.toString()[0..1]
Expand Down

0 comments on commit 9248498

Please sign in to comment.