DreamgirlH
DreamgirlH

Reputation: 1

i want button on which when click it first check the password if the password is abcd than shows the another submit button else show an alert

 <form action="#" method="post" target="_blank">
        <div id = "another sub">
        </br>
        <input type = "password" size = "25px" id="pswd">
        </br>
        </div>
        <button><a href="table.html" target="_blank" onclick="checkAddress('password');" >Submit</a></button>
<script>
        function checkAddress(field){
            var val = document.getElementById("pswd").value;
            if  (val === "abcd"){
                alert ("Matched");
            }
            else {
                alert("Try for Next Time");
            }
        }
        </script>
    </form>

i have a form here in which it first check password than takes to the another page

Upvotes: 0

Views: 1674

Answers (4)

C.Wychgel
C.Wychgel

Reputation: 1

It would be wise to not use inline js, using a examplename.js file that is loaded would be better solution.

function checkAddress(field) {
  var val = document.getElementById("pswd").value;
  if (val === "abcd") {
    alert("Matched");
    document.location.href = 'table.html';
  } else {
    alert("Try for Next Time");
  }
}
<div id="another sub">
  <input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress();">Submit</button>

Upvotes: 0

Ashwin Bhamare
Ashwin Bhamare

Reputation: 435

You have to Add your Script Tag below the form tag or you can add it at the end of the page before the body closing tag, make sure to include the js in the page

<form action="#" method="post" target="_blank">
    <div id = "another sub">
    </br>
    <input type = "password" size = "25px" id="pswd">
    </br>
    </div>
    <button onclick="checkAddress('password');">Submit</button>
</form>
 <script>
    function checkAddress(field){
        var val = document.getElementById("pswd").value;
        if  (val === "abcd"){
            alert ("Redirect To page if Matched Found");
            window.location = "http://www.redirecturlhere.com/"
        }
        else {
            alert("Try for Next Time");
        }
    }
    </script>

Upvotes: 2

Jack Bashford
Jack Bashford

Reputation: 44125

Don't use a form to avoid the reload:

function checkAddress(field) {
  var val = document.getElementById("pswd").value;
  if (val == "abcd") {
    alert("Matched");
    location.href = "table.html";
  } else {
    alert("Try for Next Time");
  }
}
<div id="another sub">
  <input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress()">Submit</button>

Upvotes: 0

Chaska
Chaska

Reputation: 3205

In your case, you don't need <form> and <a> tags. Just redirect when password is input correct.

function checkAddress(field) {
  var val = document.getElementById("pswd").value;
  if (val === "abcd") {
    alert("Matched");
    document.location.href = 'table.html';
  } else {
    alert("Try for Next Time");
  }
}
<div id="another sub">
  <input type="password" size="25px" id="pswd">
</div>
<button onclick="checkAddress();">Submit</button>

Upvotes: 0

Related Questions