javascript 使用静态密码在 Java 脚本中进行用户验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19036600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User Validation in Java Script using Static Password
提问by Ashutosh Singh
I am new to Java Script and recently got a program in JS For Implementing Static Password Protection
我是 Java Script 的新手,最近在 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 ! 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 的最佳书籍!
回答by Nicolás Straub
change onsubmit="validate()"
to onsubmit="return validate();"
.
更改onsubmit="validate()"
为onsubmit="return validate();"
.
this way, when validate returns false, the form won't submit. you'd also have to change the validate func to return false when the form doesn't validate, the resulting code would be:
这样,当验证返回 false 时,表单将不会提交。当表单未验证时,您还必须更改验证函数以返回 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 不是特殊标识符或任何东西,它们只是帮助您更好地跟踪代码的注释