为了实现所需的功能,我们需要为JavaScript添加事件监听器和正则表达式验证逻辑。以下是修改后的HTML代码,其中包括JavaScript代码段来实现这些功能。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> <style type="text/css"> *{margin: 0;padding: 0;} body{ background: url(image/bg2.jpg) no-repeat center 0; } .form{ width: 850px; margin: 150px auto; background-color: rgba(0,0,0,0.7); padding: 10px; } p{ color: #fff; line-height: 30px; border-bottom: 1px solid #ccc; padding: 10px 0; clear: left; overflow: auto; } p:first-child{ font-size: 24px; font-weight: bold; border-bottom: 2px dotted #999; } input{ width: 260px; height: 30px; margin-left: 50px; line-height: 30px; border-radius: 3px; border: none; padding: 0 5px; } #btn{ width: 79px; height: 32px; margin: 10px 150px; } .indicator { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin-left: 10px; } .valid { background-color: green; } .invalid { background-color: red; } </style> </head> <body> <div class="form"> <form action="welcome.html" method="post" id="myform"> <p>注  册</p> <p>用 户 名: <input type="text" name="user" id="user" placeholder="请输入用户名"> <span id="user-msg"></span><span class="indicator" id="user-indicator"></span> <span>仅支持数字、字母、_和-的组合</span> </p> <p>密  码: <input type="password" name="psw" id="psw" placeholder="请输入密码"> <span id="psw-msg"></span><span class="indicator" id="psw-indicator"></span> <span>包含至少一个大写字母,一个小写字母的,不少于8位</span> </p> <p>重复密码:<input type="password" name="psw2" id="psw2" placeholder="请重复密码"><span id="psw2-msg"></span><span class="indicator" id="psw2-indicator"></span></p> <p>电  话: <input type="text" name="tel" id="tel" placeholder="请输入13|15|18开头的11位电话号码"> <span id="tel-msg"></span><span class="indicator" id="tel-indicator"></span> <span>13|15|18开头的11位电话号码</span> </p> <input type="image" src="image/btn2.gif" id="btn"> </form> </div> <script type="text/javascript"> document.addEventListener('DOMContentLoaded', function() { const userInput = document.getElementById('user'); const pswInput = document.getElementById('psw'); const psw2Input = document.getElementById('psw2'); const telInput = document.getElementById('tel'); const userMsg = document.getElementById('user-msg'); const pswMsg = document.getElementById('psw-msg'); const psw2Msg = document.getElementById('psw2-msg'); const telMsg = document.getElementById('tel-msg'); const userIndicator = document.getElementById('user-indicator'); const pswIndicator = document.getElementById('psw-indicator'); const psw2Indicator = document.getElementById('psw2-indicator'); const telIndicator = document.getElementById('tel-indicator'); const userRegex = /^[a-zA-Z0-9_-]+$/; const pswRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/; const telRegex = /^(13|15|18)\d{9}$/; function validateInput(input, regex, msgElem, indicatorElem, hint) { input.addEventListener('blur', function() { if (regex.test(input.value)) { msgElem.textContent = ''; indicatorElem.classList.remove('invalid'); indicatorElem.classList.add('valid'); } else { msgElem.textContent = hint; indicatorElem.classList.remove('valid'); indicatorElem.classList.add('invalid'); } }); } validateInput(userInput, userRegex, userMsg, userIndicator, '用户名仅支持数字、字母、_和-的组合'); validateInput(pswInput, pswRegex, pswMsg, pswIndicator, '密码需包含至少一个大写字母,一个小写字母,不少于8位'); validateInput(telInput, telRegex, telMsg, telIndicator, '请输入13|15|18开头的11位电话号码'); psw2Input.addEventListener('blur', function() { if (pswInput.value === psw2Input.value) { psw2Msg.textContent = ''; psw2Indicator.classList.remove('invalid'); psw2Indicator.classList.add('valid'); } else { psw2Msg.textContent = '两次输入的密码不一致'; psw2Indicator.classList.remove('valid'); psw2Indicator.classList.add('invalid'); } }); }); </script> </body> </html> ``` 额外需要注意的逻辑点: 1. 用户体验:可以在用户开始输入时就显示一些即时的验证反馈(例如,密码强度条),而不仅仅是在输入框失去焦点时。 2. 安全性:虽然前端验证提供了良好的用户体验,但所有的验证逻辑也应在服务器端重复执行,以确保数据的安全性和完整性。 3. 可访问性:确保所有用户,包括使用屏幕阅读器的用户,都能理解验证消息和指示器。可能需要添加一些ARIA属性来提高可访问性。 [2025-06-15 18:07:04 | AI写代码神器 | 1841点数解答]