示例代码如下:
<html> <head> <meta charset="gb2312">
<title>js简单表单验证</title>
<script type="text/javascript">
window.onload=function()
{
var bt=document.getElementById("bt"); bt.onclick=function()
{
if(document.myform.name.value=="")
{
alert("用户名不能为空!"); document.myform.name.focus(); return false;
}
else if(document.myform.pw.value=="")
{
alert("密码不能为空!"); document.myform.pw.focus(); return false;
} } } </script>
</head>
<body>
<form action="index.php" method="get" name="myform"> <ul> <li>姓名:<input type="text" name="name" id="name" /></li> <li>密码:<input type="text" name="pw" id="age" /></li> <li><input type="submit" id="bt"/></li> </ul> </form> </body> </html>