0

I'm very new to Javascript. I have done one template for login, It is working like a charm. Here My question is how to set up the validation and navigation like if(username==root && password==root) then it should redirect into other page. I just post my code. I didn't include the css code. Then i did this code in html in that i have try to write the javascript but i don't know how to handle this.

My code:

<!DOCTYPE html>
<html>
<style>
/* Full-width input fields */
input[type=text], input[type=password] {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

/* Set a style for all buttons */
button {
    background-color: #0077B5;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    cursor: pointer;
    width: 100%;
}

button:hover {
    opacity: 0.8;
}

/* Extra styles for the cancel button */
.cancelbtn {
    width: auto;
    padding: 10px 18px;
    background-color: #f44336;
}

/* Center the image and position the close button */
.imgcontainer {
    text-align: center;
    margin: 10px 0 5px 0;
    position: relative;
}

img.cisco{
    width: 30%;
    border-radius: 30%;
}

.container {
    padding: 10px;
}

span.psw {
    float: right;
    padding-top: 16px;
}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    /*overflow: auto;  Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    padding-top: 5px;
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 5% auto 15% auto; /* 5% from the top, 15% from the bottom and centered */
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button (x) */
.close {
    position: absolute;
    right: 25px;
    top: 0;
    color: #000;
    font-size: 35px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: red;
    cursor: pointer;
}

/* Add Zoom Animation */
.animate {
    -webkit-animation: animatezoom 0.6s;
    animation: animatezoom 0.6s
}

@-webkit-keyframes animatezoom {
    from {-webkit-transform: scale(0)} 
    to {-webkit-transform: scale(1)}
}

@keyframes animatezoom {
    from {transform: scale(0)} 
    to {transform: scale(1)}
}

/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 200px) {
    span.psw {
       display: block;
       float: none;
    }
    .cancelbtn {
       width: 100%;
    }
    .Center {
  display: flex;
  align-items: center;
  justify-content: center;
}
}
</style>
<body>

<h2><text-align:center>Authentication Required</h2>

<button onclick="document.getElementById('id01').style.display='block'">Login</button>

<div id="id01" class="modal">

  <form class="modal-content animate" method="post" name="myform">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
      <img src="../images/cisco1.png" alt="Cisco" class="cisco">
    </div>

    <div class="container">
      <label><b>Username</b></label>
      <input type="text" id="username" placeholder="Enter Username" name="uname" required>

      <label><b>Password</b></label>
      <input type="password" id="password" placeholder="Enter Password" name="psw" required>

      <button type="submit" id="login" onclick="validate()">Login</button>
      <input type="checkbox" checked="checked"> Remember me
    </div>

    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
      <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
  </form>
</div>

<script>
    var attempt = 3; //Variable to count number of attempts

//Below function Executes on click of login button
function validate(){
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    if ( username == "root" && password == "root"){
        alert ("Login successfully");
        console.log("Redirecting to welcome page...")
        window.location = "success_new.html"; //redirecting to other page
        return false;
    }
    else{
        attempt --;//Decrementing by one
        alert("You have left "+attempt+" attempt;");
        }

        //Disabling fields after 3 attempts
        if( attempt == 0){
            document.getElementById("username").disabled = true;
            document.getElementById("password").disabled = true;
            document.getElementById("submit").disabled = true;
            return false;
        }
}
</script>

</body>
</html>
7
  • Unless you are just playing around with this for practice, I would advice against validating credentials on the front-end because anyone could theoretically manipulate that validation. Commented Nov 6, 2017 at 13:04
  • Get the value by using document.getElementById and keep it in a var and then validate.I dont know what really you are facing difficulty in this Commented Nov 6, 2017 at 13:06
  • @lalithkumar or document.getElementByNames Commented Nov 6, 2017 at 13:08
  • whatever now he has only name attributes in the form.he can add id or get the values by names Commented Nov 6, 2017 at 13:10
  • @Chris yeah i know but i have less time to report that's why i posted this question. thanks Commented Nov 6, 2017 at 13:11

2 Answers 2

1

At last I fix it. The issue here was "return" function on the onclick listener. here is that one

add return before the validate function

<button type="submit" id="login" onclick=" return validate()">Login</button>

Then it's works fine.

Sign up to request clarification or add additional context in comments.

Comments

0

var username = document.getElementById('uname');
var pwd = document.getElementById('psw');

document.getElementById("b").onclick = function(){
  if(username.value == "root" && pwd.value == "root"){
    console.log("Redirecting to welcome page...")
    window.location = "welcome.html";
  }
}
<h2 text-align:"center">Authentication Required</h2>

<button onclick="document.getElementById('id01').style.display='block';">Login</button>

<div id="id01" class="modal" style="display:none;">

  <form class="modal-content animate" action="">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
      <img src="../images/logo1.png" alt="Logo" class="logo1">
    </div>

    <div class="container">
      <label><b>Username</b></label>
      <input type="text" placeholder="Enter Username" name="uname" id="uname" required>

      <label><b>Password</b></label>
      <input type="password" placeholder="Enter Password" name="psw" id="psw" required>

      <button type="button" id="b">Login</button>
      <input type="checkbox" checked="checked"> Remember me
    </div>

    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
      <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
  </form>
</div>

<script>
// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

Fixed it. If I were you, I'd go over these and look at each difference to see what went wrong. It should be noted that you shouldn't do client-side validation like this, because anyone can easily:

  • See the password and username

  • Go to the url directly

  • Etc.

You really should do this server-side.

4 Comments

Oops . Not working for me. If i click the submit button it is not redirecting
@ArunBaskar Try it now.
What do you mean by "trying inside the script tag"?

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.