Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Credit Card Validator Tool
- * Provides comprehensive credit card validation with custom options.
- *
- * Usage:
- * var options = {
- * customCardTypes: [
- * {
- * name: 'CustomCard',
- * pattern: '^123',
- * valid_length: [16]
- * },
- * // Add more custom card types as needed
- * ],
- * useJQuery: true
- * };
- *
- * var creditCardValidator = new CreditCardValidator(options);
- * var creditCardNumber = '4111-1111-1111-1111';
- * var expirationDate = '12/25';
- * var cardholderName = 'John Doe';
- *
- * creditCardValidator.validateCreditCard(creditCardNumber, expirationDate, cardholderName, function (error, result) {
- * if (error) {
- * console.error('Validation failed:', error.message);
- * } else {
- * console.log('Validation result:', result);
- * }
- * });
- */
- var CreditCardValidator = (function () {
- function Trie() {
- this.trie = {};
- this.push = function (value) {
- value = value.toString();
- var obj = this.trie;
- for (var i = 0; i < value.length; i++) {
- var char = value[i];
- if (obj[char] == null) {
- obj[char] = (i === value.length - 1) ? null : {};
- }
- obj = obj[char];
- }
- };
- this.find = function (value) {
- value = value.toString();
- var obj = this.trie;
- for (var i = 0; i < value.length; i++) {
- var char = value[i];
- if (obj.hasOwnProperty(char)) {
- if (obj[char] === null) {
- return true;
- }
- } else {
- return false;
- }
- obj = obj[char];
- }
- };
- }
- function Range(trie1) {
- if (!(trie1 instanceof Trie)) {
- throw new Error('Range constructor requires a Trie parameter');
- }
- this.trie = trie1;
- this.rangeWithString = function (ranges) {
- if (typeof ranges !== 'string') {
- throw new Error('rangeWithString requires a string parameter');
- }
- ranges = ranges.replace(/ /g, '');
- ranges = ranges.split(',');
- for (var j = 0; j < ranges.length; j++) {
- var range = ranges[j];
- if (r = range.match(/^(\d+)-(\d+)$/)) {
- for (var n = parseInt(r[1]); n <= parseInt(r[2]); n++) {
- this.trie.push(n);
- }
- } else if (range.match(/^\d+$/)) {
- this.trie.push(range);
- } else {
- throw new Error("Invalid range '" + r + "'");
- }
- }
- return new Range(this.trie);
- };
- this.match = function (number) {
- return this.trie.find(number);
- };
- }
- function CreditCardValidator(options) {
- options = options || {};
- var trie = new Trie();
- var cardTypes = [
- {
- name: 'Visa',
- pattern: '^4',
- valid_length: [13, 16]
- },
- {
- name: 'MasterCard',
- pattern: '^(5[1-5]|2[2-7])',
- valid_length: [16]
- },
- {
- name: 'Amex',
- pattern: '^3[47]',
- valid_length: [15]
- },
- // Add more card types as needed
- ];
- var customCardTypes = options.customCardTypes || [];
- var allCardTypes = cardTypes.concat(customCardTypes);
- function normalize(number) {
- return number.replace(/[ -]/g, '');
- }
- function validateCardNumber(number) {
- for (var i = 0; i < allCardTypes.length; i++) {
- var cardType = allCardTypes[i];
- if (new RegExp(cardType.pattern).test(number)) {
- return {
- cardType: cardType.name,
- isValid: trie.find(number),
- validLengths: cardType.valid_length
- };
- }
- }
- return {
- cardType: 'Unknown',
- isValid: false,
- validLengths: []
- };
- }
- function isExpired(expirationDate) {
- var currentDate = new Date();
- var inputDate = new Date(expirationDate);
- return inputDate < currentDate;
- }
- function validateCardholderName(cardholderName) {
- return /^[a-zA-Z\s]+$/.test(cardholderName);
- }
- this.validateCreditCard = function (creditCardNumber, expirationDate, cardholderName, callback) {
- try {
- if (options.useJQuery && typeof window.jQuery === 'undefined') {
- throw new Error('jQuery is required for credit card validation');
- }
- var normalizedNumber = normalize(creditCardNumber);
- var validationResult = validateCardNumber(normalizedNumber);
- validationResult.isExpired = isExpired(expirationDate);
- validationResult.isValidCardholderName = validateCardholderName(cardholderName);
- if (typeof callback === 'function') {
- callback(null, validationResult);
- }
- return validationResult;
- } catch (error) {
- if (typeof callback === 'function') {
- callback(error, null);
- }
- return null;
- }
- };
- }
- return CreditCardValidator;
- })();
- // Help Menu
- console.log('Credit Card Validator Tool');
- console.log('---------------------------');
- console.log('Usage:');
- console.log('var options = {');
- console.log(' customCardTypes: [');
- console.log(' {');
- console.log(' name: \'CustomCard\',');
- console.log(' pattern: \'^123\',');
- console.log(' valid_length: [16]');
- console.log(' },');
- console.log(' // Add more custom card types as needed');
- console.log(' ],');
- console.log(' useJQuery: true');
- console.log('};');
- console.log('');
- console.log('var creditCardValidator = new CreditCardValidator(options);');
- console.log('var creditCardNumber = \'4111-1111-1111-1111\';');
- console.log('var expirationDate = \'12/25\';');
- console.log('var cardholderName = \'John Doe\';');
- console.log('');
- console.log('creditCardValidator.validateCreditCard(creditCardNumber, expirationDate, cardholderName, function (error, result) {');
- console.log(' if (error) {');
- console.log(' console.error(\'Validation failed:\', error.message);');
- console.log(' } else {');
- console.log(' console.log(\'Validation result:\', result);');
- console.log(' }');
- console.log('});');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement