app.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. let currentPage = 2;
  3. const tabs = document.getElementsByClassName('page');
  4. const validField = (formId, fieldId, customFieldId) => {
  5. const form = document.getElementById(formId);
  6. const element = form.querySelector(`#${fieldId}`);
  7. if (element.value === '') {
  8. form.classList.add('was-validated');
  9. return false;
  10. } else {
  11. if (customFieldId && element.value === 'other') {
  12. const customField = form.querySelector(`#${customFieldId}`);
  13. if (customField.value === '') {
  14. customField.classList.add('is-invalid');
  15. return false
  16. } else {
  17. return true
  18. }
  19. }
  20. return true;
  21. }
  22. }
  23. const conditions = {
  24. agreement: [
  25. () => {
  26. const agreementCheckbox = document.getElementById('agreement');
  27. if (agreementCheckbox.checked) {
  28. return true;
  29. } else {
  30. const form = document.getElementById('terms-and-conditions-form');
  31. form.classList.add('was-validated');
  32. return false;
  33. }
  34. }
  35. ],
  36. personalInformation: [
  37. () => validField('personal-information-form', 'age-range'),
  38. () => validField('personal-information-form', 'gender', 'gender-custom'),
  39. () => validField('personal-information-form', 'ethnicity', 'ethnicity-custom'),
  40. () => validField('personal-information-form', 'town-birth'),
  41. () => validField('personal-information-form', 'postal-code'),
  42. ]
  43. }
  44. const showCustomInput = (customElementId, element) => {
  45. if (element.value === 'other') {
  46. const customInput = document.getElementById(customElementId);
  47. customInput.style.display = 'block'
  48. }
  49. }
  50. const showPage = (page) => {
  51. tabs[page].style.display = 'block';
  52. }
  53. const isValid = condition => condition();
  54. const changePage = (page, condition) => {
  55. if (condition && !conditions[condition].map(isValid).every(result => result)) {
  56. return;
  57. }
  58. tabs[currentPage].style.display = 'none';
  59. showPage(page);
  60. currentPage = page;
  61. }
  62. showPage(2);