javascript一些问题

来源:百度知道 编辑:UC知道 时间:2024/09/20 21:42:08
<form name=form1 method=post action="" onsubmit="checkform(this); return false">
<input type=text name=password>
<input type=submit name=submit1>
</form>
<script language="javascript">
function checkform(frm)
{
if(frm.password.value.length!=6)
{
alert("密码必须是6位");
document.frm.password.focus();
return false;
}
else{
num_pass=frm.password.value;
for(var i=0;i<num_pass.length;i++)
{
if(num_pass.charAt(i)<'0' || num_pass.charAt(i)>'9'){

break;

}

}
if(i==num_pass.length)
frm.submit();
}

}
</script>

为什么我插入document.frm.password.focus();
无论验证是否错误都会提交表单呢

<form name=form1 method=post action="" onsubmit="checkform(this); return false">
<input type=text name=password>
<input type=submit name=submit1>
</form>
<script language="javascript">
function checkform(frm)
{
if(frm.password.value.length!=6)
{
alert("密码必须是6位");
document.frm.password.focus();
return false;
}
else{
num_pass=frm.password.value;
for(var i=0;i<num_pass.length;i++)
{
if(num_pass.charAt(i)<'0' || num_pass.charAt(i)>'9'){

break;

}

}
if(i==num_pass.length)
frm.submit();
}

}
</script>

break语句应该改为return false;

<form name=form1 method=post action="" onsubmit="checkform(this)>
onsubmit改为onclick