简体   繁体   English

使用静态密码的Java脚本中的用户验证

[英]User Validation in Java Script using Static Password

I am new to Java Script and recently got a program in JS For Implementing Static Password Protection 我是Java脚本的新手,最近在JS中获得了一个用于实现静态密码保护的程序

Here is my code : 这是我的代码:

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

    <script "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 ");
                continue();
            }
        else
            {
                alert("Incorrect Username or Password" );
            }

    }
    </script>
    </head>

    <body>
    <text align=center>
    <form name="form" onsubmit="validate()">
    Username <input type="text" name="username" />
    <br />
    <br />
    Password <input type="password" name="password" maxlength=10 />

    <input type="submit" />
    </form>
    </text>
    </body>

Now , I have defined a username->"sample" by default and password ->"password" by default for user validation . 现在,我为用户验证默认定义了一个用户名->“ sample”和默认密码->“ password”。

But whenever after submitting the form resets again without executing the validate function ! 但是无论何时提交表单后,都将再次重置而无需执行validate函数! As am new to JS ignore me for a silly mistake . 作为JS的新手,请忽略我一个愚蠢的错误。

Also suggest some best books for Learning JS and JSP from the scratch ! 还建议您从头开始学习JS和JSP的一些最佳书籍!

change onsubmit="validate()" to onsubmit="return validate();" onsubmit="validate()"更改为onsubmit="return validate();" .

this way, when validate returns false, the form won't submit. 这样,当validate返回false时,表单将不会提交。 you'd also have to change the validate func to return false when the form doesn't validate, the resulting code would be: 您还必须更改validate函数,以在表单不通过验证时返回false,结果代码为:

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;
            }

    }

Update: continue and break illustrated. 更新:继续并中断图示。

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

just in case, :loopStart and :loopEnd aren't special identifiers or anything, they're just comments to help you trace the code better 以防万一,:loopStart和:loopEnd不是特殊的标识符或其他任何东西,它们只是注释,可以帮助您更好地跟踪代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM