如何让我的代码接受多个用户名和密码?

时间:2018-01-22 17:30:59

标签: javascript php html forms passwords

我有一个网站,我编程"使用HTML,我找到了一个脚本(JavaScript)来获取网站维护时的密码。它目前只接受一个用户名和密码,是否可以接受多个用户名和密码? 这不是我的代码,我在网上找到它并希望添加到它。该项目纯粹是出于教育目的。

**旁注,我没有太多的经验,而且只是对这个问题进行投票的人不会帮助我学习如何做到这一点。我是高中的学生,仍在学习这些语言,唯一的学习方法就是在需要时获得帮助。

<form name="login">
    Username  <input type="text" name="userid" />
    Password  <input type="password" name="pswrd" />
    <input type="button" onclick="check(this.form)" value="Login" />
    <button formaction="/index.html">Cancel</button>
</form>
<script language="javascript">
    function check(form)/*function to check userid & password*/
    {
         /*the following code checkes whether the entered userid and password are matching*/
         if(form.userid.value == "example-username" && form.pswrd.value == "example-password")
         {
             window.open("/mainpage.html")/*opens the target page while Id & password matches*/
         }
         else
         {
             alert("Error Password or Username")/*displays error message*/
         }
    }
</script>

1 个答案:

答案 0 :(得分:0)

看一下这个链接:[如果声明] [1]

[1]:https://www.w3schools.com/js/js_comparisons.asp在逻辑运算符部分下。您需要将该逻辑应用于以下行

if(form.userid.value == "example-username" && form.pswrd.value == "example-password")

基本上,在第二个人的逻辑运算符之后复制条件

if(form.userid.value == "example-username" && form.pswrd.value == "example-password" || form.userid.value == "example-username2" && form.pswrd.value == "example-password2")

作为Chris Stated,这绝不是安全的,需要后端系统和数据库来处理登录检查。在这种格式下,任何人都可以只查看JS代码并获取密码,或者在网址中输入www.example.com/mainpage.html以完全绕过您的登录。