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

214
Views
Javascript field validation code not working

This code isn't working. If email and password are empty, display "Please enter your email and password." If just email is empty, display "Please enter your email." If just password is empty, display "Please enter your password." If none are empty, display no error message. That is the functionality behind this code, however.. it isn't working. What's happening? Code:

   <script type='text/javascript'>
    
    var loginEmail = document.getElementById('ema');
    var loginPassword = document.getElementById('pswd');
    var loginBtn = document.getElementById('loginBtn');
    var loginError = document.getElementById('loginError');
    
    loginBtn.addEventListener("click", function(e) {
      e.preventDefault();
      if(loginEmail.value === "" || loginPassword.value === "") {
        loginError.style.visibility = 'visible';
        loginError.innerText = "Please enter your email and password.";
      } else if (loginEmail.value === "" && loginPassword.value.length > 0) {
        loginError.style.visibility = 'visible';
        loginError.innerText = "Please enter your email.";
      } else if (loginPassword.value === "" && loginEmail.value.length > 0) {
        loginError.style.visibility = 'visible';
        loginError.innerText = "Please enter your password.";
      } else {
        loginError.style.visibility = 'hidden';
      }
    });
    
    </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is the expression of the first if. You want to display the message "Please enter your email and password" if both fields are empty, but you're using a logical or ||. By using a logical and && it will work, and the expression of the else ifs can be simplified:

var loginEmail = document.getElementById('ema');
var loginPassword = document.getElementById('pswd');
var loginBtn = document.getElementById('loginBtn');
var loginError = document.getElementById('loginError');

loginBtn.addEventListener("click", function(e) {
  e.preventDefault();
  if(loginEmail.value === "" && loginPassword.value === "") {
    loginError.style.visibility = 'visible';
    loginError.innerText = "Please enter your email and password.";
  } else if (loginEmail.value === "") {
    loginError.style.visibility = 'visible';
    loginError.innerText = "Please enter your email.";
  } else if (loginPassword.value === "") {
    loginError.style.visibility = 'visible';
    loginError.innerText = "Please enter your password.";
  } else {
    loginError.style.visibility = 'hidden';
  }
});
<input type="text" id="ema">
<input type="password" id="pswd">
<button id="loginBtn">Login</button>
<div id="loginError" style="visibility: hidden">
</div>

about 4 years ago · Juan Pablo Isaza Report
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!