如何循环检查用户输入密码的次数? [英] How to check with loops how many times user punched a password?

查看:62
本文介绍了如何循环检查用户输入密码的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的家庭作业中,我必须设置一个测试,要求用户输入密码(三遍).如果用户输入错误密码三次,那么我必须显示一条消息密码错误".此外,我还必须考虑用户名和密码的三种变体

On my homework assignment I have to set up a test that ask the user for the password ( three times). If the user punches wrong password three times, then I must display a message " Wrong password". Also I have to take into account three variants of the username and password

我想我设置了预定义的密码,如test"、password"等.我会对用户名做同样的事情.所以基本上我只知道如何计算用户输入用户名和密码的次数......

I am thinking that I set up a predefined passwords like "test", "password" and so on. The same I will do with the usernames. So basically I just know how to count how many times the user punches the username and password...

<!DOCTYPE html>
<html>

  <body>

  <script>

  var password = "";

  while(password = "" || password != "password"||username !="Bella"||username="") {
username=prompt ("What´s your username?);    
passord = prompt("What´s the password?");
  }

  alert("That was correct!");

  </script>

  </body>
</html>

用户名是什么?密码是多少?

What´s the username? What´s the password?

错误 - 输入 3 次这是正确的

wrong - typed 3 times That was correct

推荐答案

<!DOCTYPE html>
<html>

  <body>

  <script>
    var incorrectCount = 0;
    var correctPassword = "randompassword"
    function checkPassword(){
        var enteredPassword  = document.getElementById("txtpassword").value;
        if(enteredPassword != correctPassword)
            incorrectCount++
        else{
            incorrectCount = 0
            document.getElementById("userMessage").innerHTML = "Correct Password !!"
        }

        if(incorrectCount == 3){
            document.getElementById("userMessage").innerHTML = "Incorrect password entered 3 or more times"
            document.getElementById("userMessage").style.visibility = "visible";
        }
    }
  </script>

  <input type="text" id="txtpassword" name ="txtpassword">
  <input type="button" id="loginbutton" value="Login" onclick="checkPassword()"><br>
  <label id="userMessage" style="visibility:hidden;"></label>
  </body>
</html>

  1. 您不需要为此使用循环.
  2. 您可以只拥有一个全局变量,并在每次用户输入错误密码时递增它.
  3. 一旦计数器达到 3,您就可以显示消息.
  4. 请参考上面的代码.

另外,尝试搜索诸如 - 如何验证输入、如何显示/显示消息 - 这将有助于您学习

这篇关于如何循环检查用户输入密码的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆