Skip to content

Commit

Permalink
add a static format card method
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-stripe committed Feb 15, 2013
1 parent 3ceeb10 commit 11adfc1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/jquery.payment.js

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

23 changes: 23 additions & 0 deletions src/jquery.payment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,77 @@ $.fn.payment = (method, args...) ->

# Utils

defaultFormat = /(\d{1,4})/g

cards = [
{
type: 'maestro'
pattern: /^(5018|5020|5038|6304|6759|676[1-3])/
format: defaultFormat
length: [12..19]
cvcLength: [3]
luhn: true
}
{
type: 'dinersclub'
pattern: /^(36|38|30[0-5])/
format: defaultFormat
length: [14]
cvcLength: [3]
luhn: true
}
{
type: 'laser'
pattern: /^(6706|6771|6709)/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: true
}
{
type: 'jcb'
pattern: /^35/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'unionpay'
pattern: /^62/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: false
}
{
type: 'discover'
pattern: /^(6011|65|64[4-9]|622)/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'mastercard'
pattern: /^5[1-5]/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'amex'
pattern: /^3[47]/
format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/
length: [15]
cvcLength: [3..4]
luhn: true
}
{
type: 'visa'
pattern: /^4/
format: defaultFormat
length: [13..16]
cvcLength: [3]
luhn: true
Expand Down Expand Up @@ -387,3 +399,14 @@ $.payment.validateCardCVC = (cvc, type) ->
$.payment.cardType = (num) ->
return null unless num
cardFromNumber(num)?.type or null

$.payment.formatCardNumber = (num) ->
card = cardFromNumber(num)
return num unless card

if card.format.global
num.match(card.format)?.join(' ')
else
groups = card.format.exec(num)
groups?.shift()
groups?.join(' ')

0 comments on commit 11adfc1

Please sign in to comment.