利用javascript对提交数据验证


优点:提交前验证、在客户端进行。

<html>
<head>
<script language="javascript"> 
function checkinfo()
{
if(document.form1.yonghu.value=="") 
{
alert("请输入用户名后提交!");
document.form1.yonghu.focus();
return false;
}else return true;
} 
</script> 
</head>
<body>
<form action="main_page.jsp" method="post" name="form1" onsubmit="return checkinfo()">
<input type="submit" name="denglu" value="登录..">
<input type="button" name="zhuce" value="注册.." onclick="location.href='registor.jsp';">
</form>
</body>


说明:

onsubmit="return XXXX()"

与onsubmit="XXXX()"的区别是
前者执行了javascript且可以根据返回值确定是否提交并跳转;而后者仅执行了javascript不跳转;

之前所写,迁移至此

原文链接:http://user.qzone.qq.com/372806800/blog/1336200998

原文地址:https://www.cnblogs.com/amwuau/p/6255370.html