<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> </style> </head> <body> <form action="http://www.it666.com"> <input type="text" name="" id="" placeholder="请输入账号" name="userName"> <input type="password" name="" id="" placeholder="请输入密码" name="userPwd"> <input type="submit" value="注册"> </form> <script type="text/javascript"> /* 需求: 1.账号和密码必须大于等于6位 2.如果账号密码的长度不够就改变input输入框的背景颜色 3.如果用户输入的账号或者密码不符合需求, 那么就不能提交 */ let oUserName =document.querySelector("input[type=text]"); let oPassword = document.querySelector("input[type=password]"); let oSubmit =document.querySelector("input[type=submit]"); oSubmit.onclick=function() { // 1.账号和密码必须大于等于6位 if(oUserName.value.length<6) { oUserName.style.background="yellow"; return false; } else { oUserName.style.background="#fff"; } if (oPassword.value.length<6) { oPassword.style.background="red"; return false; } else { oPassword.style.background="#fff"; } } </script> </body> </html>
dom啦16 表单校验 核心在于:一样先做完再另一样把。慢慢纠正把。
关注
打赏