user3487121
user3487121

Reputation: 53

display message on wrong password entry

I have a submit button which only works when "victory" is typed into the form. Now I am trying to work on an error message to display "wrong keyword entry" if the text entered into the form field isn't "victory". here is the code

<form name="input" action="index.html" method="post" onsubmit="return check();">
<input type="text" maxlength="7" autocomplete="off" name="" id="victory">

<br><input type="submit" class="bigbutton"  value="NEXT">

</form>    
<script>

function check(){
if(document.getElementById("victory").value == "victory")
    return true;
else 
   return false;

}

Upvotes: 1

Views: 22177

Answers (2)

ElliotSchmelliot
ElliotSchmelliot

Reputation: 8362

If I were you I'd add an HTML element to stuff an error into. Then you can style it with CSS however you'd like.

<div id="error"></div>

Then your function would look something like this:

function check(){
    if(document.getElementById("victory").value == "victory")
        return true;
    else 
        document.getElementById("error").innerHTML = "Wrong keyword entry."
        return false;
}

Upvotes: 3

T J
T J

Reputation: 43156

You can simply add the alert in the else condition…

function check(){
   if(document.getElementById("victory").value == "victory") {
        return true;
   }
   else {    
        alert("wrong keyword entry");
        return false;
   }
 }

Upvotes: 1

Related Questions