Skip to content

Instantly share code, notes, and snippets.

@drewtempelmeyer
Created January 11, 2014 20:57
Show Gist options
  • Save drewtempelmeyer/8376668 to your computer and use it in GitHub Desktop.
Save drewtempelmeyer/8376668 to your computer and use it in GitHub Desktop.
jQuery Validation methods for Balanced.js
/**
* Balanced.js Validators
*
* jQuery Validation methods for use with Balanced.js
*/
;(function($) {
"use strict";
/**
* Validates the format of the bank account routing number
*/
$.validator.addMethod('routingNumber', function(value, element) {
return balanced.bankAccount.validateRoutingNumber(value);
}, 'Please enter a valid routing number.');
/**
* Validates the bank account type (either "checking" or "savings")
*/
$.validator.addMethod('accountType', function(value, element) {
return balanced.bankAccount.validateType(value);
}, 'Please choose a valid account type.');
/**
* Validates the format of the credit card number
*/
$.validator.addMethod('creditCard', function(value, element) {
return balanced.creditCard.isCardNumberValid(value);
}, 'Please enter a valid credit card number.');
/**
* Validates the CVV2 code against the credit card number
*
* Provide a data-credit-card attribute with the id of the credit card number
* field. If the attribute is absent, we'll assume it's valid if it >= 3 && <= 4
* characters in length.
*/
$.validator.addMethod('cvvCode', function(value, element) {
if (!$(element).hasData('credit-card')) {
return value.length >= 3 && value.length <= 4;
}
var cardNumber = $('#' + $(element).data('credit-card')).val();
return balanced.creditCard.isSecurityCodeValid(cardNumber, value);
}, 'Please enter a valid CVV2 code.');
$.validator.addClassRules({
routingNumber: { routingNumber: true },
accountType: { accountType: true },
creditCard: { creditCard: true },
cvvCode: { cvvCode: true }
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment