给你写个简单的,一个文本框,一个密码框,验证两个都不能为空的。
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function checkForm()
{
var f = document.forms[0];
if(f.username.value=='')
{
document.getElementById("userNameInfo").style.display="block";
return false;
}
if(f.password.value=='')
{
document.getElementById("passwordInfo").style.display="block";
return false;
}
return true;
}
</script>
</head>
<body>
<form action="#" method="post" onsubmit="return checkForm()">
用户名:<input type="text" name="username" /><div id="userNameInfo" style="display:none;"><font color="red">用户名不能为空</font></div><br/>
密码:<input type="password" name="password" /><div id="passwordInfo" style="display:none;"><font color="red">密码不能为空</font></div><br/>
<input type="submit" value="登录" />
</form>
</body>
</html>