js实现form表单非空验证

时间:2022-11-06 22:05:14
<!doctype html>
<html>
<meta content="utf-8">
<head>
<script language="javascript">
function checklogin()
    {
        if((myform.user.value!=""))
        {
            if((myform.pwd.value!=""))
            {
                return true
            }
            else
            {
                alert("密码不能为空");
                return false
                }
            }
        else
        {
            alert("用户名不能为空");
            return false
            }
        }

</script>
<title></title>
</head>
<body>
<form action="form.html" name="myform" method="post" onSubmit="return checklogin()">

<input type="text" name="user" size="16"><br>
<input type="password" name="pwd" size="16"><input type="submit" value="登录" name="sub_1">
</form>

</body>

</html>