<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.bg {
background-color: yellow;
}
</style>
</head>
<body>
<input type="text" id="name"><br>
<input type="text" id="pw"><br>
<input type="button" id="btn" value="提交">
<script>
// 检测用户名是否是3-6位,密码是否是6-8位,如果不满足要求高亮显示文本框
// 1.获取元素
var username = document.getElementById("name");
var pw = document.getElementById("pw");
var btn = document.getElementById("btn");
// 2.给按钮添加点击事件,然后判断位数是否足够
btn.onclick = function () {
// 用户名是否是3-6位,不满足高亮显示
if (username.value.length < 3 || username.value.length > 6) {
username.className = "bg";
// 输入错误直接跳出程序,防止提交错误数据
return;
} else {
username.className = "";
}
// 密码是否是6-8位,不满足高亮显示
if (pw.value.length < 6 || pw.value.length > 8) {
pw.className = "bg";
return;
} else {
pw.className = "";
}
// 模拟提交
console.log("提交数据");
};
</script>
</body>
</html>