Skip to content

Commit 9376e4a

Browse files
authored
Merge pull request payolapayments#262 from alexeymetelev/client-side-validation
Add client side validation
2 parents 634879d + 1589e7f commit 9376e4a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to Payola will be documented in this file.
1111
- Raise error if `payola_can_modify_customer/subscription?` unimplemented. #246
1212

1313
### Enhancements
14+
- Add client side validation to subscription_form_onestep.js
1415
- Unpegged Stripe gem and stripe-ruby-mock. #255
1516
- Take optional `stripe_customer_id` when creating a sale. #183
1617
- Clean up error target HTML attributes. #198

app/assets/javascripts/payola/subscription_form_onestep.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ var PayolaOnestepSubscriptionForm = {
66
},
77

88
handleSubmit: function(form) {
9+
if (!PayolaOnestepSubscriptionForm.validateForm(form)) {
10+
return false;
11+
}
12+
913
$(form).find(':submit').prop('disabled', true);
1014
$('.payola-spinner').show();
1115
Stripe.card.createToken(form, function(status, response) {
@@ -14,6 +18,29 @@ var PayolaOnestepSubscriptionForm = {
1418
return false;
1519
},
1620

21+
validateForm: function(form) {
22+
var cardNumber = $( "input[data-stripe='number']" ).val();
23+
if (!Stripe.card.validateCardNumber(cardNumber)) {
24+
PayolaOnestepSubscriptionForm.showError(form, 'The card number is not a valid credit card number.');
25+
return false;
26+
}
27+
28+
var expMonth = $( "select[data-stripe='exp_month']" ).val();
29+
var expYear = $( "select[data-stripe='exp_year']" ).val();
30+
if (!Stripe.card.validateExpiry(expMonth, expYear)) {
31+
PayolaOnestepSubscriptionForm.showError(form, "Your card's expiration month/year is invalid.");
32+
return false;
33+
}
34+
35+
var cvc = $( "input[data-stripe='cvc']" ).val();
36+
if(!Stripe.card.validateCVC(cvc)) {
37+
PayolaOnestepSubscriptionForm.showError(form, "Your card's security code is invalid.");
38+
return false;
39+
}
40+
41+
return true;
42+
},
43+
1744
stripeResponseHandler: function(form, status, response) {
1845
if (response.error) {
1946
PayolaOnestepSubscriptionForm.showError(form, response.error.message);

0 commit comments

Comments
 (0)