<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"/>
<style type="text/css">
</style>
</head>
<body>
<!--登陆时候,验证账号密码是否为空-->
<!--onsubmit="return login()"提交时候触发判断-->
<form action="...." method="post" onsubmit="return login()">
用户名:<input id="name" type="text"/><br>
密码:<input id="pass" type="password"/>
<input type="submit" value="提交">
</form>
<script>
function login(){
//得到name输入框对象
var name = document.getElementById("name");
//判断输入框是否有内容
if(name.value.length==0){
confirm("用户名不能为空");
return false;
}
var pass = document.getElementById("pass");
if(pass.value.length==0){
confirm("密码不能为空");
return false;
}
return true;
}
</script>
</body>
</html>js常用,登录时候验证用户名密码是否为空,为空则不提交的行为
最新推荐文章于 2022-09-25 15:40:36 发布
本文介绍了一个简单的HTML表单验证示例,通过JavaScript实现对用户名和密码字段的非空检查,确保用户在提交表单前已正确填写必要信息。
2万+

被折叠的 条评论
为什么被折叠?



