我是Java Script的新手,最近用JS编写了一个实现静态密码保护的程序
下面是我的代码:
<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>
现在,我已经定义了用户名->默认的“sample”和默认的密码->" password“用于用户验证。
但是每当提交表单后,都会在不执行验证函数的情况下再次重置!作为JS的新手,忽略我的一个愚蠢的错误。
也推荐一些从零开始学习JS和JSP的最好的书籍!
发布于 2013-09-27 03:14:15
将onsubmit="validate()"更改为onsubmit="return validate();"。
这样,当validate返回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;
}
}更新:继续和中断图解。
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以防万一,:loopStart和:loopEnd不是特殊的标识符或任何东西,它们只是帮助您更好地跟踪代码的注释
https://stackoverflow.com/questions/19036600
复制相似问题