Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda