<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表单验证</title>
</head>
<body>
<form action="" method="get" onsubmit="return checkForm()">
<div align="center">
<h2>用户登录</h2>
<hr width="30%"/>
账号:<input type="text" name="userid" id="userid" value="" /><br /><br />
密码:<input type="password" name="password" id="password" value="" /><br /><br />
<input type="submit" value="用户登录" />
</div>
</form>
</body>
<script type="text/javascript">
//验证表单
function checkForm(){
//账号的信息
var userid = document.getElementById("userid");
//密码的信息
var password = document.getElementById("password");
if(userid.value.length == 0){
alert("账号不能为空");
return false;
}
if(password.value.length == 0){
alert("密码不能为空");
return false;
}
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表单验证</title>
</head>
<body>
<form action="" method="get" id="f1">
<div align="center">
<h2>用户登录</h2>
<hr width="30%"/>
账号:<input type="text" name="userid" id="userid" value="" /><br /><br />
密码:<input type="password" name="password" id="password" value="" /><br /><br />
<input type="submit" value="用户登录" />
</div>
</form>
</body>
<script type="text/javascript">
window.onload = function(){
document.getElementById("f1").onsubmit = function(){
//账号的信息
var userid = document.getElementById("userid");
//密码的信息
var password = document.getElementById("password");
if(userid.value.length == 0){
alert("账号不能为空");
return false;
}
if(password.value.length == 0){
alert("密码不能为空");
return false;
}
}
}
</script>
</html>