暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

modal-add-new-cc.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Add new credit card
  3. */
  4. 'use strict';
  5. document.addEventListener('DOMContentLoaded', function (e) {
  6. (function () {
  7. // Variables
  8. const creditCardMask = document.querySelector('.credit-card-mask'),
  9. expiryDateMask = document.querySelector('.expiry-date-mask'),
  10. cvvMask = document.querySelector('.cvv-code-mask'),
  11. btnReset = document.querySelector('.btn-reset');
  12. if (creditCardMask) {
  13. creditCardMask.addEventListener('input', event => {
  14. creditCardMask.value = formatCreditCard(event.target.value);
  15. const cleanValue = event.target.value.replace(/\D/g, '');
  16. let type = getCreditCardType(cleanValue);
  17. if (type && type !== 'unknown' && type !== 'general') {
  18. document.querySelector('.card-type').innerHTML =
  19. '<img src="' + assetsPath + 'img/icons/payments/' + type + '-cc.png" class="cc-icon-image" height="20"/>';
  20. } else {
  21. document.querySelector('.card-type').innerHTML = '';
  22. }
  23. });
  24. registerCursorTracker({
  25. input: creditCardMask,
  26. delimiter: ' '
  27. });
  28. // reset card image on click of cancel
  29. btnReset.addEventListener('click', function (e) {
  30. // blank '.card-type' innerHTML to remove image
  31. document.querySelector('.card-type').innerHTML = '';
  32. });
  33. }
  34. // Expiry Date Mask
  35. if (expiryDateMask) {
  36. expiryDateMask.addEventListener('input', event => {
  37. expiryDateMask.value = formatDate(event.target.value, {
  38. date: true,
  39. delimiter: '/',
  40. datePattern: ['m', 'y']
  41. });
  42. });
  43. registerCursorTracker({
  44. input: expiryDateMask,
  45. delimiter: '/'
  46. });
  47. }
  48. // CVV
  49. if (cvvMask) {
  50. cvvMask.addEventListener('input', event => {
  51. const cleanValue = event.target.value.replace(/\D/g, '');
  52. cvvMask.value = formatNumeral(cleanValue, {
  53. numeral: true,
  54. numeralPositiveOnly: true
  55. });
  56. });
  57. }
  58. // Credit card form validation
  59. FormValidation.formValidation(document.getElementById('addNewCCForm'), {
  60. fields: {
  61. modalAddCard: {
  62. validators: {
  63. notEmpty: {
  64. message: 'Please enter your credit card number'
  65. }
  66. }
  67. }
  68. },
  69. plugins: {
  70. trigger: new FormValidation.plugins.Trigger(),
  71. bootstrap5: new FormValidation.plugins.Bootstrap5({
  72. // Use this for enabling/changing valid/invalid class
  73. // eleInvalidClass: '',
  74. eleValidClass: '',
  75. rowSelector: '.form-control-validation'
  76. }),
  77. submitButton: new FormValidation.plugins.SubmitButton(),
  78. // Submit the form when all fields are valid
  79. // defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
  80. autoFocus: new FormValidation.plugins.AutoFocus()
  81. },
  82. init: instance => {
  83. instance.on('plugins.message.placed', function (e) {
  84. //* Move the error message out of the `input-group` element
  85. if (e.element.parentElement.classList.contains('input-group')) {
  86. e.element.parentElement.insertAdjacentElement('afterend', e.messageElement);
  87. }
  88. });
  89. }
  90. }).on('plugins.message.displayed', function (e) {
  91. if (e.element.parentElement.classList.contains('input-group')) {
  92. //* Move the error message out of the `input-group` element
  93. e.element.parentElement.insertAdjacentElement('afterend', e.messageElement.parentElement);
  94. }
  95. });
  96. })();
  97. });