Created
August 13, 2012 01:47
-
-
Save lfborjas/3336379 to your computer and use it in GitHub Desktop.
some validations for a credit card form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery -> | |
for id, callback of Model.validations | |
[selector, event] = id.split("on") | |
$(selector).on(event, callback) | |
Model = | |
error_template: _.template( | |
'<span class="billing-error"><%= msg %></span>' | |
) | |
validate: (opts)-> | |
el = if opts.query? then $(opts.query) else $(opts.el) | |
unless opts.validator(opts.accessor(el)...) | |
el.addClass("error").closest(".control-group").addClass("error").append( | |
Model.error_template(msg: opts.error_message) | |
) | |
Model.validations = | |
"#ccn on focusout": -> | |
Model.validate( | |
validator: Stripe.validateCardNumber | |
accessor: (el) -> [el.val()] | |
error_message: "Invalid credit card number" | |
el: this | |
) | |
"#cvc on focusout": -> | |
Model.validate( | |
validator: Stripe.validateCVC | |
accessor: (el) -> [el.val()] | |
error_message: "Invalid CVC" | |
el: this | |
) | |
".date on change": -> | |
Model.validate( | |
validator: Stripe.validateExpiry | |
accessor: (el) -> | |
["month", "year"].map (part)-> | |
el.find("#card_#{part}").val() | |
error_message: "Invalid date" | |
query: "#expiry_date" | |
) | |
"input, select on focusin": -> | |
$(this).removeClass("error").closest(".control-group") | |
.removeClass("error") | |
.find( | |
".billing-error" | |
).remove() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment