A1raa
A1raa

Reputation: 625

Javascript else if statement not working with form validation

I'm trying to validate a form that will send an email. At the moment the button returns formCheck() onclick. Which is meant to display a popup respective of field completetion.

I'm new to JS so I'm having a little trouble working out what I'm doing wrong as the outcome is always the else "Thanks".

   <script>
        function formCheck() {
            if (document.getElementById("Name") === "")
            {
                alert("please enter name");
            }
            else if (document.getElementById("Email") === "")
            {
                alert("Please enter an email address");
            }
            else if (document.getElementById("Name") && document.getElementById("Email") === "")
            {
                alert("Please enter a Name and Email address");
            }
            else {
                alert("Thanks");
            }
        }
    </script>

To me it looks like I'm either not using an if statementcorrectly or its not picking up the fields are empty when defined as "". If anybody can point me in the right direction it would be much appreciated.

Upvotes: 0

Views: 244

Answers (1)

Lucas Rodrigues
Lucas Rodrigues

Reputation: 1192

You should be comparing the value instead of the object itself:

document.getElementById("Name").value

Upvotes: 4

Related Questions