Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
how to execute function after validation?

I have a form and validation for it. The problem is that I don't know how to check if the fields are filled in correctly and then send. How can I submit after all fields are filled in correctly?

const addBanksFunc = () => {
  addBankButton.addEventListener("click", (event) => {
    event.preventDefault();
    checkInputs();
  });
};

const setError = (element, message) => {...};

const setSuccess = (element) => {...};

const checkInputs = () => {
  if (bankName.value === "") {
    setError(bankName, "Some text");
  } else {
    setSuccess(bankName);
  }

  if (interestRate.value === "") {
    setError(interestRate, "Some text");
  } else {
    setSuccess(interestRate);
  }

  if (maximumLoan.value === "") {
    setError(maximumLoan, "Some text");
  } else {
    setSuccess(maximumLoan);
  }

  if (minimumDownPayment.value === "") {
    setError(minimumDownPayment, "Some text");
  } else {
    setSuccess(minimumDownPayment);
  }

  if (loanTerm.value === "") {
    setError(loanTerm, "Some text");
  } else {
    setSuccess(loanTerm);
  }
};

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Just change your checkInputs signature, make it return true (form is valid) or false.

const checkInputs = () => {
  if (bankName.value === "") {
    setError(bankName, "Some text");
    return false;
  } else {
    setSuccess(bankName);
  }

  if (interestRate.value === "") {
    setError(interestRate, "Some text");
    return false;
  } else {
    setSuccess(interestRate);
  }
  ...

 return true;
}

and

const addBanksFunc = () => {
  addBankButton.addEventListener("click", (event) => {
    event.preventDefault();
    const isFormValid = checkInputs();
    if (isFormValid) {
        // submit the form here
    }
  });
};

Im actually not really sure about how to // submit the form with button onClick listener, probably it is better to use something like

orderForm.on('submit', function(event) {
    event.preventDefault();
    //some code here
    this.submit();
})
about 4 years ago · Juan Pablo Isaza Relatório

0

get the return values from all the validation functions that you are executing and if all of them are true then submit the form

about 4 years ago · Juan Pablo Isaza Relatório

0

Add an id or class for the form you want to submit. Lets say you have put an id="myForm" to the for you want to submit.

After all the checks, use the below code to submit the form through code.

document.getElementById("myForm").submit();

Note: put the submit button outside the form to avoid auto submission without the checks.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda