%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/camaservice.eco-n-tech.co.uk/wp-content/themes/camaservice/src/js/custom/
Upload File :
Create Path :
Current File : /var/www/camaservice.eco-n-tech.co.uk/wp-content/themes/camaservice/src/js/custom/step-form.js

initStepForm();

function initStepForm() {
  const form = document.getElementById("step-form");
  const progressNumber = Array.from(document.querySelectorAll("#step-form .step-progress__item"));
  const steps = Array.from(document.querySelectorAll("#step-form .step-block"));
  const nextBtn = document.querySelectorAll("#step-form .next-btn");
  const prevBtn = document.querySelectorAll("#step-form .previous-btn");

  nextBtn.forEach((button) => {
    button.addEventListener("click", () => {
      changeStep("next");
    });
  });
  prevBtn.forEach((button) => {
    button.addEventListener("click", () => {
      changeStep("prev");
    });
  });

  function changeStep(btn) {
    let index = 0;

    const active = document.querySelector(".step-block.active");

    if (btn === "next") {
      if (!validateForm(active)) return false;
    }

    index = steps.indexOf(active);
    steps[index].classList.remove("active");

    if (btn === "next") {
      index++;
      progressNumber[index].classList.add("active");
      if (index > 0) {
        progressNumber[index - 1].classList.add("done");
      }
    } else if (btn === "prev") {
      progressNumber[index].classList.remove("active");
      index--;
      if (index > 0) {
        progressNumber[index].classList.remove("done");
      }
    }
    steps[index].classList.add("active");
    if (index == 0) {
      progressNumber[0].classList.remove("done");
    }

    form.scrollIntoView({
      behavior: "smooth",
    });
  }

  function validateForm(active) {
    let valid = true;
    let inputs = active.querySelectorAll('[aria-required="true"]');

    for (i = 0; i < inputs.length; i++) {
      if (inputs[i].value == "") {
        inputs[i].style.borderColor = "red";
        inputs[i].addEventListener("focus", (event) => {
          event.target.style.borderColor = "";
        });

        valid = false;
      } else if (inputs[i].getAttribute("type") == "email") {
        valid = validateEmail(inputs[i].value);
        if (!valid) {
          inputs[i].style.borderColor = "red";
        }
      } else {
        inputs[i].style.borderColor = "";
      }
    }

    return valid;
  }

  function validateEmail(email) {
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
  }
}

Zerion Mini Shell 1.0