Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!