0

Whenever I click on my button it always says "please enter an email" even though there is text in the box. What am I doing wrong? I see that the ID for the password is incorrect. Right Now Im going to try and put the variables in the function.

var emailText = document.getElementById("Email").value;
var emailErrorMessage = document.getElementById("emailError");
var passwordText = document.getElementById("Password").value;
var passwordErrorMessage = document.getElementById("passwrodError");
var button = document.getElementById("submit");

function buttonValidation1(){
    var text;

    if (emailText === "") {
      text = "Please enter an email";
    }else{
      text = "Email OK";
    }

    emailErrorMessage.innerHTML = text;
}

button.addEventListener("click", buttonValidation1);


  <!DOCTYPE html>
     <html>
       <head>

        </head>
     <!--<body>
        <p>Please input a number between 1 and 10:</p>

        <input id="numb">

        <button type="button" id="submit">Submit</button>

         <p id="demo"></p> -->

        <label>Email: &nbsp; &nbsp; &nbsp;</label>
        <input type="text" id="Email"></input><span id="emailError"></span>
        <br/> <br/>

        <label>Password:</label>
        <input type="password" id="Password"></input>
        <spanid="passwordError"></span>
        <br/>
        <input type="submit" id="submit"></input>
        <script src="Form.js"></script>
        </body>
        </html>
2
  • 1
    Add you HTML code please. Commented Dec 15, 2016 at 23:38
  • 3 === means boolean comparison, maybe only use 2 if you want to do a string comparison. Commented Dec 15, 2016 at 23:40

1 Answer 1

2

Move all variable into body function

function buttonValidation1(){
    var emailText = document.getElementById("Email").value;
    var emailErrorMessage = document.getElementById("emailError");
    var passwordText = document.getElementById("Password").value;
    var passwordErrorMessage = document.getElementById("passwrodError");
    var button = document.getElementById("submit");
    
    var text;
    
    if (emailText === "") {
        text = "Please enter an email";
    }
    
    else {
        text = "Email OK";
    }
    
    emailErrorMessage.innerHTML = text;
}

button.addEventListener("click", buttonValidation1);
Sign up to request clarification or add additional context in comments.

3 Comments

I think this will fix it, because the emailText will be set when the function is called rather than before it's defined.
Yeah that fixed it. But I dont know what I had to use the three equal signs to get it to work. it was giving me an error when i only used two equal signs and set it equal to "". It would allow me to use two equal signs and
" " with a space in between the quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.