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

336
Visualizações
How to Do Form Validation for Inputs Using Fetch API?

So I have this app where I have to add a valid address in order to get a delivery fee.

My Problem is if I don't fill out any of the inputs and click my button, it will just show the error in my console, but the user won't know what happened and the function runs regardless of any inputs being added or not.

How do I make my button not run the function unless the inputs are filled out? Also, how do I display the error message on my browser?

Here is my Function

async function getFee() {
  const payload = getFormValues();
  const finalPayload = JSON.stringify(payload);

  const response = await fetch("/get-fee", {
    method: "POST",
    body: finalPayload,
    headers: { "Content-Type": "application/json" },
  })
    .then(async (response) => {
      const resp = await response.json();
      return resp;
    })
    .catch((rejected) => {
      console.log(rejected);
    });
}

Then I have my button in my HTML

 <button id="btn1" onclick="getFee()">
      Calculate Delivery Fee
    </button>

The problem is if I try to add required to my inputs, it does nothing since I am connecting to an API and using the data from my inputs to get the delivery fee.

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

0

Without seeing the form HTML I can only guess. But you could try the built-in HTML attribute for inputs, "required". https://www.w3schools.com/html/html_form_attributes.asp

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