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

189
Visualizações
Front end form validation

I'm confused right now. It seems I'm missing something right here. Trying to submit the form, the code for the email field was running through the username. For instance, if I click on the submit button while the input for username and email were empty, the code for email, "email cannot be blank" would run through the username field. I need your response on this.

const form = document.getElementById("form");
const username = document.getElementById("username");
const email = document.getElementById("email");
const password = document.getElementById("password");
const confirmpass = document.getElementById("re-password");

form.addEventListener("submit", (e)=>{
  e.preventDefault();
  Checkvalue();
})

function Checkvalue(){
  const userval= username.value.trim();
  const emailval =  email.value.trim();
  const passwordval= password.value.trim();
  const confirmpassval = confirmpass.value.trim();
  
  if (userval == '') {
    setErrorFor(username, 'username cannot be blank');
  }else{
    setSuccessFor(username);
  }
  
  if (emailval=='') {
    setErrorFor(email, 'enter your mail');
  }else if (!isEmail(emailval)) {
    setErrorFor(emailval, "email is not valid");
  } else {
    setSuccessFor(email);
  }
}

function setErrorFor(input, message){
  const formContol = input.parentElement;
  const small = document.querySelector('small');
  
  small.innerText = message;
  formContol.className = 'form-contol error'
}

function setSuccessFor(input){
  const formContol = input.parentElement;
  formContol.className = 'form-contol success'
}

function isEmail(email){
  const re =  /\S+@\S+\. \S+/;
  return re.test(email);
}
about 4 years ago · Juan Pablo Isaza
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