如何在javascript中编写验证代码

时间:2017-11-06 13:02:56

标签: javascript html login

我对Javascript很新。我已经为登录做了一个模板,它就像一个魅力。在这里,我的问题是如何设置验证和导航,如if(username == root&& password == root),那么它应该重定向到其他页面。我只是发布我的代码。我没有包含css代码。然后我在html中做了这个代码,我试着写javascript但我不知道如何处理这个。

我的代码:

<!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>

2 个答案:

答案 0 :(得分:1)

最后我修好了。这里的问题是&#34;返回&#34; onclick监听器上的函数。这是一个

在验证函数

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

然后它的工作正常。

答案 1 :(得分: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>

修正了它。如果我是你,我会仔细检查每个差异,看看出了什么问题。应该注意的是,您不应该像这样进行客户端验证,因为任何人都可以轻松地:

  • 查看密码和用户名

  • 直接转到网址

你真的应该做这个服务器端。