提交验证是否为空-js代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function check(){
    var name2=document.formname.name1;
    var password2=document.formname.password1;
    if(name2.value==""){
        alert("用户名不能为空");
        name2.focus();
        return false;
    }
    if(password2.value==""){
        alert("密码不能为空")
        password2.focus();
        return false;
    }
}
</script>
</head>

<body>
<form action="5秒后跳转.html" name="formname" method="get" onsubmit="return check()">
用户名:<input type="text" name="name1"  /><br />
密码:<input type="password" name="password1"  /><br />
<input type="submit" name="提交1" value="提交" />
</form>
</body>
</html>
原文地址:https://www.cnblogs.com/yunmengxiaohe/p/3864067.html