설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pages-auth-two-steps.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Page auth two steps
  3. */
  4. 'use strict';
  5. document.addEventListener('DOMContentLoaded', function () {
  6. // Self-executing function to initialize event listeners and form validation
  7. (() => {
  8. const maskWrapper = document.querySelector('.numeral-mask-wrapper');
  9. const twoStepsForm = document.querySelector('#twoStepsForm');
  10. if (maskWrapper) {
  11. // Loop through each child of the mask wrapper
  12. Array.from(maskWrapper.children).forEach(pin => {
  13. pin.addEventListener('keyup', e => {
  14. // Only handle numeric keys or backspace
  15. if (/^\d$/.test(e.key)) {
  16. // Move focus to the next field if maxlength is reached
  17. if (pin.nextElementSibling && pin.value.length === parseInt(pin.getAttribute('maxlength'))) {
  18. pin.nextElementSibling.focus();
  19. }
  20. } else if (e.key === 'Backspace') {
  21. // Move focus to the previous field on backspace
  22. if (pin.previousElementSibling) {
  23. pin.previousElementSibling.focus();
  24. }
  25. }
  26. });
  27. pin.addEventListener('keypress', e => {
  28. // Prevent entering the minus key
  29. if (e.key === '-') {
  30. e.preventDefault();
  31. }
  32. });
  33. });
  34. }
  35. // Form validation for OTP
  36. if (twoStepsForm) {
  37. const numeralMaskList = twoStepsForm.querySelectorAll('.numeral-mask');
  38. // Keyup handler to update OTP value
  39. const keyupHandler = () => {
  40. let otpComplete = true;
  41. let otpValue = '';
  42. numeralMaskList.forEach(maskElement => {
  43. if (maskElement.value === '') {
  44. otpComplete = false;
  45. }
  46. otpValue += maskElement.value;
  47. });
  48. twoStepsForm.querySelector('[name="otp"]').value = otpComplete ? otpValue : '';
  49. };
  50. numeralMaskList.forEach(maskElement => {
  51. maskElement.addEventListener('keyup', keyupHandler);
  52. });
  53. // Initialize form validation if FormValidation is defined
  54. if (typeof FormValidation !== 'undefined') {
  55. FormValidation.formValidation(twoStepsForm, {
  56. fields: {
  57. otp: {
  58. validators: {
  59. notEmpty: {
  60. message: 'Please enter OTP'
  61. }
  62. }
  63. }
  64. },
  65. plugins: {
  66. trigger: new FormValidation.plugins.Trigger(),
  67. bootstrap5: new FormValidation.plugins.Bootstrap5({
  68. eleValidClass: '',
  69. rowSelector: '.form-control-validation'
  70. }),
  71. submitButton: new FormValidation.plugins.SubmitButton(),
  72. defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
  73. autoFocus: new FormValidation.plugins.AutoFocus()
  74. }
  75. });
  76. }
  77. }
  78. })();
  79. });