简单的前端表单的校验

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			function checkForm(){
				
				//获取用户名及其内容
				var usernameObj=document.getElementById("username");
				var username=usernameObj.value;
				//判断value是否为空 若为空不能提交表单,
				if(username==null || username == ""){
					alert("用户名不能为空");
					return false;
				}
				
				//获取密码及其内容
				//1.获取密码元素
				var pwdObj=document.getElementById("password");
				
				//2.获取密码的值
				var pwdValue=pwdObj.value;
				
				//3.判断
				if(pwdValue==null || pwdValue==""){
					alert("密码不能为空");
					return false;
				}
				
				return true;
			}
		</script>
	</head>
	<body>
		<form action="#" method="get" onsubmit="return checkForm()">
			姓名:<input name="username" id="username"/><br>
			密码:<input type="password" name="password" id="password"><br>
			<input type="submit" value="保存"/>
			<input type="reset" />
		</form>
	</body>
</html>

posted @ 2017-04-17 14:52  御前提笔小书童  阅读(213)  评论(0编辑  收藏  举报