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

507
Visualizações
When the form is empty, pressing the Login button opens the page. I have to make a mistake

When the form is empty, pressing the Login button opens the page. I want him to make a mistake. I'm already getting an error if it's not compatible with the server. How can I solve this problem ?

const login = (e) => {
    e.preventDefault();
    axiox.post("http://localhost:2000/api/auth/login", {
        email,
        password
    }).then((response) => {
        console.log("response", response)
        localStorage.setItem("login", JSON.stringify({
            userLogin: true,
            token: response.data.access_token,
        }));
        setError("");
        setEmail("");
        setPassword("");
        history.push("schools");
    })
    .catch((error) => setError(error.response.data.message));
    
};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

use this to validate the form

const login = (e) => {
    e.preventDefault();
    if(!email || !password){
     //send error message to user  
     }else{
       // make request to api
     }

};

about 4 years ago · Juan Pablo Isaza Relatório

0

A simple and efficient way is to check any errors at the client-side i.e if the form is empty you should not make an API call. The validation on the form or in your function to check if the values are null is enough.

You can add more validations on the form, however, certain validations which are business mandate could be done at the server-side.

const login = (e) => {
    e.preventDefault();
       // make request to api
   if(!email && !password) { 
      //here make the API call to authenticate a user  
   }

};

Check this link to get more details, how to add the validation in React Forms.

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you want the form to respond with an error message when they haven't filled up the form. Here:

const login = (e) => {
    e.preventDefault();
    if(!email || !password){
     //Send the error message, like alert('Either the Email or Password is empty!') 
     }else{
       //The code to execute when both Email and Password are filled.
     }
};
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