【问题标题】:How to code the validation in javascript如何在 javascript 中编写验证代码
【发布时间】:2017-11-06 13:02:56
【问题描述】:

我对 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>

【问题讨论】:

  • 除非你只是为了练习而玩这个,否则我建议不要在前端验证凭据,因为理论上任何人都可以操纵该验证。
  • 使用 document.getElementById 获取值并将其保存在 var 中,然后进行验证。我不知道您在这方面遇到了什么困难
  • @lalithkumar 或document.getElementByNames
  • 无论现在他在表单中只有名称属性。他可以添加id或通过名称获取值
  • @Chris 是的,我知道,但我没有多少时间来报告这就是我发布这个问题的原因。谢谢

标签: javascript html login


【解决方案1】:

我终于修好了。这里的问题是 onclick 侦听器上的“返回”功能。这是那个

在验证函数前添加return

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

然后它工作正常。

【讨论】:

    【解决方案2】:

    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>

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

    • 查看密码和用户名

    • 直接进入网址

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

    【讨论】:

    • 哎呀。不为我工作。如果我单击提交按钮,它不会重定向
    • @ArunBaskar 立即尝试。
    • “在脚本标签内尝试”是什么意思?
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2012-05-22
    • 2016-11-24
    • 1970-01-01
    • 2010-10-11
    • 2011-05-03
    相关资源
    最近更新 更多