下面这个是很简单地检查
如果读懂了稍为修改一下即可实现上面功能
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>158</title>
<script language="javascript">
function isEmpty(str)
{
for(var intLoop=0;intLoop<str.length;intLoop++)
if(""!=str.charAt(intLoop))
return false;
return true;
}function checkRequired(f)
{
var strError="";
for(var intLoop=0;intLoop<f.elements.length;intLoop++)
if(null!=f.elements[intLoop].getAttribute("required"))
if(isEmpty(f.elements[intLoop].value))
strError+=" "+f.elements[intLoop].name+"\n"; if(""!=strError)
{
alert("要求的信息没有输入:\n"+strError);
return false;
}
}
</script>
</head>
<body>
<h1>用户信息</h1>
<form name=userinfo onsubmit="return checkRequired(this);">
<p>在提交表单之前必须正确输入用户名和E-Mail地址</p>
用户名: &nbsp;
<input type=text name="用户名" required> <br>
E-Mail 地址:<input type="text" name="E-Mail 地址" required><br>
年龄 (可选):<input type="text" name="Age"><br>
<input type=submit value="发送"></form>
</body>
</html>