<!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:
为了实现所需的功能,我们需要为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点数解答]
- [百度文心] <!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: (1841点数解答 | 2025-06-15 18:07:04)137
- [字节豆包] <!DOCTYPE html> <html> <head> <style> .three-line-table { border-collapse: collapse; width: 100%; margin: 20px 0; font-family: Arial, sans-serif; } .three-line-table th, .three-line-table td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; } .three-line-table th { border-top: 2px solid #333; border-bottom: 2px solid #333; background-color: #f5f5f5; } .three-line-table tr:last-child td { border-bottom: 2px solid #333; } a { color: #0066cc; text-decoratio(75点数解答 | 2025-03-13 23:16:59)233
- [DeepSeek] <!DOCTYPE html> <html> <head> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: auto; border: 2px solid #e0e0e0; padding: 20px } .header { text-align: center; border-bottom: 2px solid #c00; padding-bottom: 10px } .section { margin: 15px 0 } table { width: 100%; border-collapse: collapse } th, td { border: 1px solid #ddd; padding: 8px; text-align: left } .diagnosis { background: #fff8e1; padding: 15px; border-left: 4px solid #ffc107 } .signatur(223点数解答 | 2025-06-10 13:32:12)154
- [DeepSeek] <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>大学生"时间银行"商业计划书</title> <style> body { font-family: "微软雅黑", sans-serif; line-height: 1.6; max-width: 1200px; margin: 0 auto; padding: 20px; } h1 { color: #2c3e50; border-bottom: 3px solid #3498db; } h2 { color: #3498db; margin-top: 30px; } table { width: 100%; border-collapse: collapse; margin: 20px 0; } th, td { border: 1px solid #ddd; padding: 12px; text-align: left; } (593点数解答 | 2025-05-24 19:26:34)143
- [字节豆包] <!DOCTYPE html> <html> <head> <style> body { font-family: Arial, sans-serif; background: #f5f5f5; } .container { max-width: 500px; margin: 20px auto; padding: 20px; background: white; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } .header { text-align: center; font-size: 24px; font-weight: bold; margin-bottom: 20px; color: #333; } .flavor { display: flex; justify-content: space-between; margin: 15px 0; padding: 10px; border-bottom: 1px dashed #ee(1102点数解答 | 2025-06-23 18:32:52)156
- [字节豆包] <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>SC1030皮卡车后驱动桥设计论文</title> <style> body { font-family: SimSun; line-height: 1.5; } h1 { text-align: center; font-size: 22pt; } h2 { font-size: 16pt; margin-top: 20pt; } table { border-collapse: collapse; width: 100%; margin: 20px 0; } th, td { border: 1px solid #000; padding: 8px; text-align: left; } img { max-width: 100%; display: block; margin: 10px auto; } </style> </head> <body> <h1>SC1030皮卡车后驱动桥设计</h1> <h2>摘要</h2> (111点数解答 | 2025-03-03 12:53:25)260
- [DeepSeek] <!DOCTYPE html> <html> <head> <style> body { font-family: 'Arial', sans-serif; background: #fff5f7; padding: 20px; } .container { max-width: 600px; margin: 0 auto; background: white; border-radius: 15px; padding: 25px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); } .title { color: #ff3366; font-size: 24px; text-align: center; padding: 15px; background: #fff0f3; border-radius: 10px; margin-bottom: 20px; } .tip-card { background: #f8f9fa; border-left: 4px solid #f(308点数解答 | 2025-02-26 23:11:57)282
- [DeepSeek] <!DOCTYPE html> <html> <head> <style> body { background: #fff5f7; font-family: "Microsoft YaHei", sans-serif; padding: 20px; } .container { max-width: 600px; margin: 0 auto; background: white; border-radius: 15px; padding: 25px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); } .title { color: #ff4b6e; font-size: 24px; font-weight: bold; margin-bottom: 15px; text-align: center; } .subtitle { color: #666; font-size: 14px; margin-bottom: 20px; text-align: center; } .(338点数解答 | 2025-02-26 22:59:16)291
- [字节豆包] <!DOCTYPE html> <html> <head> <style> body { margin: 20px; font-family: Arial; } .container { width: 800px; height: 600px; border: 2px solid #ccc; position: relative; } .area { position: absolute; border-radius: 10px; padding: 10px; } /* 轮椅体验区 */ #wheelchair { width: 400px; height: 120px; background: #e6f7ff; top: 20px; left: 20px; } #turning { width: 150px; height: 150px; background: #e6f7ff; top: 140px; left: 20px; border-radius: 50%; } /* 拐杖区 */ #crutch { width: 300px;(1089点数解答 | 2025-06-05 23:25:44)303
- [字节豆包] <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body { font-family: '仿宋', 'SimSun'; line-height: 1.8; margin: 40px; } .header { text-align: center; font-weight: bold; font-size: 20px; margin-bottom: 30px; } .party-info { margin: 15px 0; } .requests, .facts { margin: 20px 0; } .signature { text-align: right; margin-top: 50px; } .stamp-placeholder { text-align: right; color: #999; font-size: 14px; margin-top: 10px; } </style> </head> <body> <div class="(1077点数解答 | 2025-11-26 16:56:05)43
- [字节豆包] ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>孙浩 - Java开发工程师</title> <style> body { font-family: '微软雅黑', sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; } .header { text-align: center; border-bottom: 2px solid #2c3e50; padding-bottom: 10px; } h1 { color: #2c3e50; margin: 5px 0; } .contact { display: flex; justify-content: center; gap: 20px; margin: 10px 0; } .contact a { color: #3498db; text-deco(185点数解答 | 2025-02-21 14:06:35)223
- [字节豆包] <!DOCTYPE html> <html> <head> <title>PCSP金属盘-AR泡沫玻璃板安装仿真</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } .diagram { background: #f5f5f5; padding: 15px; margin: 10px 0; border-radius: 5px; } pre { background: #eee; padding: 10px; overflow-x: auto; } table { border-collapse: collapse; width: 100%; margin: 15px 0; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } button { padding: 8px 12px; margin: 5px(1571点数解答 | 2025-06-16 21:26:05)163