Advertisement
xGHOSTSECx

Credit Card Generator

Dec 24th, 2023
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.11 KB | None | 0 0
  1. * Credit Card Validator Tool
  2. * Provides comprehensive credit card validation with custom options.
  3. *
  4. * Usage:
  5. * var options = {
  6. *   customCardTypes: [
  7. *     {
  8. *       name: 'CustomCard',
  9. *       pattern: '^123',
  10. *       valid_length: [16]
  11. *     },
  12. *     // Add more custom card types as needed
  13. *   ],
  14. *   useJQuery: true
  15. * };
  16. *
  17. * var creditCardValidator = new CreditCardValidator(options);
  18. * var creditCardNumber = '4111-1111-1111-1111';
  19. * var expirationDate = '12/25';
  20. * var cardholderName = 'John Doe';
  21. *
  22. * creditCardValidator.validateCreditCard(creditCardNumber, expirationDate, cardholderName, function (error, result) {
  23. *   if (error) {
  24. *     console.error('Validation failed:', error.message);
  25. *   } else {
  26. *     console.log('Validation result:', result);
  27. *   }
  28. * });
  29. */
  30. var CreditCardValidator = (function () {
  31. function Trie() {
  32. this.trie = {};
  33. this.push = function (value) {
  34. value = value.toString();
  35. var obj = this.trie;
  36. for (var i = 0; i < value.length; i++) {
  37. var char = value[i];
  38. if (obj[char] == null) {
  39. obj[char] = (i === value.length - 1) ? null : {};
  40. }
  41. obj = obj[char];
  42. }
  43. };
  44. this.find = function (value) {
  45. value = value.toString();
  46. var obj = this.trie;
  47. for (var i = 0; i < value.length; i++) {
  48. var char = value[i];
  49. if (obj.hasOwnProperty(char)) {
  50. if (obj[char] === null) {
  51. return true;
  52. }
  53. } else {
  54. return false;
  55. }
  56. obj = obj[char];
  57. }
  58. };
  59. }
  60. function Range(trie1) {
  61. if (!(trie1 instanceof Trie)) {
  62. throw new Error('Range constructor requires a Trie parameter');
  63. }
  64. this.trie = trie1;
  65. this.rangeWithString = function (ranges) {
  66. if (typeof ranges !== 'string') {
  67. throw new Error('rangeWithString requires a string parameter');
  68. }
  69. ranges = ranges.replace(/ /g, '');
  70. ranges = ranges.split(',');
  71. for (var j = 0; j < ranges.length; j++) {
  72. var range = ranges[j];
  73. if (r = range.match(/^(\d+)-(\d+)$/)) {
  74. for (var n = parseInt(r[1]); n <= parseInt(r[2]); n++) {
  75. this.trie.push(n);
  76. }
  77. } else if (range.match(/^\d+$/)) {
  78. this.trie.push(range);
  79. } else {
  80. throw new Error("Invalid range '" + r + "'");
  81. }
  82. }
  83. return new Range(this.trie);
  84. };
  85. this.match = function (number) {
  86. return this.trie.find(number);
  87. };
  88. }
  89. function CreditCardValidator(options) {
  90. options = options || {};
  91. var trie = new Trie();
  92. var cardTypes = [
  93. {
  94. name: 'Visa',
  95. pattern: '^4',
  96. valid_length: [13, 16]
  97. },
  98. {
  99. name: 'MasterCard',
  100. pattern: '^(5[1-5]|2[2-7])',
  101. valid_length: [16]
  102. },
  103. {
  104. name: 'Amex',
  105. pattern: '^3[47]',
  106. valid_length: [15]
  107. },
  108. // Add more card types as needed
  109. ];
  110. var customCardTypes = options.customCardTypes || [];
  111. var allCardTypes = cardTypes.concat(customCardTypes);
  112. function normalize(number) {
  113. return number.replace(/[ -]/g, '');
  114. }
  115. function validateCardNumber(number) {
  116. for (var i = 0; i < allCardTypes.length; i++) {
  117. var cardType = allCardTypes[i];
  118. if (new RegExp(cardType.pattern).test(number)) {
  119. return {
  120. cardType: cardType.name,
  121. isValid: trie.find(number),
  122. validLengths: cardType.valid_length
  123. };
  124. }
  125. }
  126. return {
  127. cardType: 'Unknown',
  128. isValid: false,
  129. validLengths: []
  130. };
  131. }
  132. function isExpired(expirationDate) {
  133. var currentDate = new Date();
  134. var inputDate = new Date(expirationDate);
  135. return inputDate < currentDate;
  136. }
  137. function validateCardholderName(cardholderName) {
  138. return /^[a-zA-Z\s]+$/.test(cardholderName);
  139. }
  140. this.validateCreditCard = function (creditCardNumber, expirationDate, cardholderName, callback) {
  141. try {
  142. if (options.useJQuery && typeof window.jQuery === 'undefined') {
  143. throw new Error('jQuery is required for credit card validation');
  144. }
  145. var normalizedNumber = normalize(creditCardNumber);
  146. var validationResult = validateCardNumber(normalizedNumber);
  147. validationResult.isExpired = isExpired(expirationDate);
  148. validationResult.isValidCardholderName = validateCardholderName(cardholderName);
  149. if (typeof callback === 'function') {
  150. callback(null, validationResult);
  151. }
  152. return validationResult;
  153. } catch (error) {
  154. if (typeof callback === 'function') {
  155. callback(error, null);
  156. }
  157. return null;
  158. }
  159. };
  160. }
  161. return CreditCardValidator;
  162. })();
  163. // Help Menu
  164. console.log('Credit Card Validator Tool');
  165. console.log('---------------------------');
  166. console.log('Usage:');
  167. console.log('var options = {');
  168. console.log('  customCardTypes: [');
  169. console.log('    {');
  170. console.log('      name: \'CustomCard\',');
  171. console.log('      pattern: \'^123\',');
  172. console.log('      valid_length: [16]');
  173. console.log('    },');
  174. console.log('    // Add more custom card types as needed');
  175. console.log('  ],');
  176. console.log('  useJQuery: true');
  177. console.log('};');
  178. console.log('');
  179. console.log('var creditCardValidator = new CreditCardValidator(options);');
  180. console.log('var creditCardNumber = \'4111-1111-1111-1111\';');
  181. console.log('var expirationDate = \'12/25\';');
  182. console.log('var cardholderName = \'John Doe\';');
  183. console.log('');
  184. console.log('creditCardValidator.validateCreditCard(creditCardNumber, expirationDate, cardholderName, function (error, result) {');
  185. console.log('  if (error) {');
  186. console.log('    console.error(\'Validation failed:\', error.message);');
  187. console.log('  } else {');
  188. console.log('    console.log(\'Validation result:\', result);');
  189. console.log('  }');
  190. console.log('});');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement