JS简单验证问题

请各位大神给写一个简单的JS验证,新手求!!没分了

<form action="#" method="post" target="_self">
<input name="password" type="text" id="password" size="20" maxlength="20" />
<input type="submit" name="Submit2" value="登陆" class="input" />
</form>

当password=11 的时候 跳转到 1.html 页面,
如果password为空的时候返回当前页,提示"不能为空"
如果password≠11的时候返回当前页,提示"输入错误"

<script type="text/javascript">
function validate(){

var n=document.getElementById("password").value;
if(n==""){
alert("密码不能为空");
return false;
}

else if(n !="11") {

alert("密码错误");
return false;
}
else if(n=="11"){

return true;
}

}
</script>

<body>
<form action="MyHtml2.html" method="post" target="_self" onsubmit="return validate()">
<input name="password" type="text" id="password" size="20" maxlength="20" />
<input type="submit" name="Submit2" value="登陆" class="input" />
</form>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-26
<script type="text/javascript">
function check(){

var n=document.getElementById("password").value;
if(n==""){
alert("不能为空");
return false;
}

else if(n !="11") {

alert("输入错误");
return false;
}
else if(n=="11"){
window.location.href="1.html";
}

}
</script>
第2个回答  2012-07-03
<script type="text/javascript">
//判断空值还有多空格现象,这是一个判断js空值的方法!为空或全是空格则返回true;
function trim(o){
if(o==null || o ==""){ return true; } var s = ""; for(var i = 0; i <o.length;i++){ s+=" "; } if(s==o){
return true;
}
return false;
}

function checkCode(){

var userpass=document.getElementById("password").value;
if(trim(userpass)){
alert("密码不能为空");
return false;
}

if(userpass !="11") {
alert("密码错误");
return false;
}
if(userpass=="11"){
document.form1.target="_self";
document.form1.action="1.html";
document.form1.submit();
}

}
</script>

<body>
<!--此处form表单修改了下-->
<form name="form1" method="post" >
<input name="password" type="text" id="password" size="20" maxlength="20" />
<input type="button" name="Submit2" value="登陆" class="input" onclick="checkCode()"/>
</form>
</body>
<html>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网