1
function validateForm() {
    if (document.getElementById('name').value = "") {
        alert('Name field is required!');
        document.getElementById('name').focus();
        return false;
    } else if (document.getElementById('email').value = "") {
        alert("Email field is required!");
        document.getElementById('email').focus();
        return false;
    } else if (document.getElementById('password1').value != document.getElementById('password2').value) {
        alert("Passwords have to match!");
        return false;
    }
    return true;
}

HTML :

<form id="form1" method="post" onsubmit="return validateForm()" action="success.html">

Why is it always returning true, even if one of these fields are empty?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
phil330d
  • 31
  • 1
  • 4
  • As for why using assignment (`=`) instead of `==` or `===` causes that to happen above: `if (x = "")` assigns `""` to `x` and then tests the assigned value. Since `""` is falsy, the condition is false. – T.J. Crowder Dec 18 '17 at 11:24

0 Answers0