Created
April 18, 2015 08:30
-
-
Save hassanjamal/cac4e09a63bf34e5b475 to your computer and use it in GitHub Desktop.
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
$(document).ready(function() { | |
$('#checkout-wizard') | |
.formValidation({ | |
framework: 'bootstrap', | |
icon: { | |
valid: 'fa fa-check', | |
invalid: 'fa fa-times', | |
validating: 'fa fa-refresh' | |
}, | |
fields: { | |
card_number: { | |
selector: '[data-stripe="number"]', | |
validators: { | |
notEmpty: { | |
message: 'The credit card number is required' | |
}, | |
creditCard: { | |
message: 'The credit card number is not valid' | |
} | |
} | |
}, | |
card_cvc: { | |
selector: '[data-stripe="cvc"]', | |
validators: { | |
notEmpty: { | |
message: 'The CVV number is required' | |
}, | |
cvv: { | |
message: 'The value is not a valid CVV', | |
creditCardField: 'card_number' | |
} | |
} | |
} | |
}, | |
expMonth: { | |
selector: '[data-stripe="exp-month"]', | |
validators: { | |
notEmpty: { | |
message: 'The expiration month is required' | |
}, | |
digits: { | |
message: 'The expiration month can contain digits only' | |
}, | |
callback: { | |
message: 'Expired', | |
callback: function(value, validator) { | |
value = parseInt(value, 10); | |
var year = validator.getFieldElements('expYear').val(), | |
currentMonth = new Date().getMonth() + 1, | |
currentYear = new Date().getFullYear(); | |
if (value < 0 || value > 12) { | |
return false; | |
} | |
if (year == '') { | |
return true; | |
} | |
year = parseInt(year, 10); | |
if (year > currentYear || (year == currentYear && value > currentMonth)) { | |
validator.updateStatus('expYear', 'VALID'); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
} | |
}, | |
expYear: { | |
selector: '[data-stripe="exp-year"]', | |
validators: { | |
notEmpty: { | |
message: 'The expiration year is required' | |
}, | |
digits: { | |
message: 'The expiration year can contain digits only' | |
}, | |
callback: { | |
message: 'Expired', | |
callback: function(value, validator) { | |
value = parseInt(value, 10); | |
var month = validator.getFieldElements('expMonth').val(), | |
currentMonth = new Date().getMonth() + 1, | |
currentYear = new Date().getFullYear(); | |
if (value < currentYear || value > currentYear + 100) { | |
return false; | |
} | |
if (month == '') { | |
return false; | |
} | |
month = parseInt(month, 10); | |
if (value > currentYear || (value == currentYear && month > currentMonth)) { | |
validator.updateStatus('expMonth', 'VALID'); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
} | |
} | |
}) | |
.on('success.validator.fv', function(e, data) { | |
if (data.field === 'card_number' && data.validator === 'creditCard') { | |
var $icon = data.element.data('fv.icon'); | |
// data.result.type can be one of | |
// AMERICAN_EXPRESS, DINERS_CLUB, DINERS_CLUB_US, DISCOVER, JCB, LASER, | |
// MAESTRO, MASTERCARD, SOLO, UNIONPAY, VISA | |
switch (data.result.type) { | |
case 'AMERICAN_EXPRESS': | |
$icon.removeClass().addClass('form-control-feedback fa fa-lg fa-cc-amex'); | |
break; | |
case 'DISCOVER': | |
$icon.removeClass().addClass('form-control-feedback fa fa-lg fa-cc-discover'); | |
break; | |
case 'MASTERCARD': | |
case 'DINERS_CLUB_US': | |
$icon.removeClass().addClass('form-control-feedback fa fa-lg fa-cc-mastercard'); | |
break; | |
case 'VISA': | |
$icon.removeClass().addClass('form-control-feedback fa fa-lg fa-cc-visa'); | |
break; | |
default: | |
$icon.removeClass().addClass('form-control-feedback fa fa-lg fa-credit-card'); | |
break; | |
} | |
} | |
}) | |
.on('err.field.fv', function(e, data) { | |
if (data.field === 'card_number') { | |
var $icon = data.element.data('fv.icon'); | |
$icon.removeClass().addClass('form-control-feedback fa fa-times'); | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment