JavaScript用户名和密码验证

时间:2015-07-25 07:17:59

标签: javascript php html

我正在尝试使用用户名和密码作为输入,如果输入的用户名和密码是admin admin,我想将它们转发到新的php文件。我不明白我哪里错了。任何帮助。提前谢谢

 <html>
 <head>
 <script type="text/javascript">
function validate()
{
    window.alert("called");
var user=document.getelementbyId(log).value;
var pass=document.getelementbyId(password).value;
window.alert("stored");
if((user=="admin")&&(pass="admin"))
{
    window.alert("logging");
    window.location.href='edusculpt_admin.php';
    }
else
    window.alert("Username or Password Incorrect");
}
</script>
</head>
<body>
    <h3>Admin Login</h3>
<form method="post">
    <p>
        Login ID: <input type="text" id="log" value=""
            placeholder="Username or Email">
    </p>
    <p>
        Password: <input type="password" id="password" value=""
            placeholder="Password">
    </p>
    <input type="submit" value="Login" onclick="validate()">
</form>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

Javascript区分大小写,getelementbyId应为getElementById,且ID必须用引号括起来。

<script type="text/javascript">
function validate()
{
    window.alert("called");
    var user=document.getElementById('log').value;
    var pass=document.getElementById('password').value;
    window.alert("stored");
    if((user=="admin")&&(pass=="admin"))
    {
        window.alert("logging");
        window.location.href='edusculpt_admin.php';
    }
    else
        window.alert("Username or Password Incorrect");
}
</script>

另请注意,您的表单中有提交按钮.. validate函数无法处理,您可以<input type="button" ...validate方法处理事件。

答案 1 :(得分:0)

getelementbyId应为getElementById&amp;用引号

括起ID名称
var user=document.getElementById("log").value;
var pass=document.getElementById("password").value;

==代替=

进行比较
if((user=="admin")&&(pass=="admin"))
                         ^^^

答案 2 :(得分:0)

onclick="validate()"更改为onclick="return validate();"

这样,当validate返回false时,表单不会单击。当表单未验证时,您还必须更改validate func以返回false,结果代码为:

<html>
    <head>
    <title>
    User Validation  : 2nd Program
    </title>

    <script type="text/javascript">

   function validate()
    {
    alert(form.username.value)
    alert(document.getelementbyId(username).value);
    alert(form.password.value)
        if(form.username.value == "sample" && form.password.value =="password")
            {
                alert("User Validated ");
                return true;
            }
        else
            {
                alert("Incorrect Username or Password" );
                return false;
            }

    }
    </script>
    </head>

    <h3>Admin Login</h3>
<form method="post">
    <p>
        Login ID: <input type="text" id="log" value=""
            placeholder="Username or Email">
    </p>
    <p>
        Password: <input type="password" id="password" value=""
            placeholder="Password">
    </p>
    <input type="submit" value="Login" onclick="validate()">
</form>
</body>
    </text>
    </body>

答案 3 :(得分:0)

尝试这个

while(true) {
    // :loopStart
    var randomNumber = Math.random();
    if (randomNumber < .5) {
        continue; //skips the rest of the code and goes back to :loopStart
    }
    if (randomNumber >= .6) {
        break; //exits the while loop (resumes execution at :loopEnd)
    }
    alert('value is between .5 and .6');
}
// :loopEnd

更新:继续并打破插图。

StringBuilder SB= new     StringBuilder();

foreach (char ch in theDate1)
{
    SB.Append(ch);
    SB.Append(" ");
}
var result =SB.ToString().TrimEnd();