html和javascript在文本框中验证密码

html和javascript在文本框中验证密码,javascript,php,html,Javascript,Php,Html,我试图使一个网站的人输入一个代码,他们得到重定向到一个页面 重要的是,每个代码都会将用户带到不同的页面 对于exsample,代码123将您带到index.html,代码000将用户带到login.html 这是我到目前为止所做的,但它不起作用。。。我做错了什么 index.html: <form id="form_id" method="post" name="myform"> <br> <br> <br> <label>Code :

我试图使一个网站的人输入一个代码,他们得到重定向到一个页面

重要的是,每个代码都会将用户带到不同的页面 对于exsample,代码123将您带到index.html,代码000将用户带到login.html

这是我到目前为止所做的,但它不起作用。。。我做错了什么

index.html:

<form id="form_id" method="post" name="myform">
<br>
<br>
<br>
<label>Code :</label>
<input type="password" name="password" id="password"/>
<input type="button" value="Login" id="submit" onclick="validate()"/>

实际情况是,表单中提交按钮的默认点击操作会将您带到重定向URL,即此页面本身,因为您没有提到操作属性

要做到这一点,当您不希望用户登录时,必须从validate函数返回false


单击函数返回false将确保不会执行默认的单击操作处理。

您可以使用jQuery来完成此操作,只需删除onclick=validation即可

login.js

function validate(){

var password = document.getElementById("password").value;
 if ( password == "1234" or "0000" or "4444" ){
alert ("Login successfully");
I dont know what to do now or is this script is even correct.
    $(function () {

    $('body').on('click', '#submit', function () {
        var password = $("#password").val();
        if (password == "1234" || password == "0000" || password == "4444") {
            alert("Login successfully");
        }

    })
})

在JSFiddle上查看这个 这样就可以了

document.getElementByIdlogin.addEventListenerclick,functionevt{ var code输入=document.getElementByIdpassword.value.trim; //对于每个不同的代码,请使用location.href重定向用户 开关代码输入{ 案例000: location.href=index.html; 打破 案例123: location.href=login.html; 打破 违约: 错误代码; } }; 代码: 登录
也许您所追求的是php解决方案:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_POST['password'] == '000') {
        header('Location: ' . 'login.html');
        exit;
    }
    if ($_POST['password'] == '123') {
        header('Location: ' . 'index.html');
        exit;
    }
}
?>

但是我如何使每一个代码都进入不同的页面呢?允许用户使用他们想要的。
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_POST['password'] == '000') {
        header('Location: ' . 'login.html');
        exit;
    }
    if ($_POST['password'] == '123') {
        header('Location: ' . 'index.html');
        exit;
    }
}
?>