目录结构
index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link rel="stylesheet" href="css/styles.css">
<script defer src="js/scripts.js"></script>
</head>
<body>
<div class="container">
<h1>用户注册</h1>
<form id="registration-form" novalidate>
<div class="form-group">
<label for="username">*用户名:</label>
<input type="text" id="username" name="username" required pattern="^[a-zA-Z0-9]{3,5}$" title="用户名由3-5个字符组成">
<span class="form-hint">用户名由3-5个字符组成</span>
</div>
<div class="form-group">
<label for="password">*密码:</label>
<input type="password" id="password" name="password" required minlength="8" maxlength="12">
<span class="form-hint">请输入8-12位密码</span>
</div>
<div class="form-group">
<label for="confirm-password">*确认密码:</label>
<input type="password" id="confirm-password" name="confirm-password" required>
<span class="form-hint">两次密码不一致</span>
</div>
<div class="form-group">
<label for="email">*Email:</label>
<input type="email" id="email" name="email" required>
<span class="form-hint">格式示例:xxxxxxxx@163.com</span>
</div>
<div class="form-group">
<label for="phone">*手机号码:</label>
<input type="tel" id="phone" name="phone" required pattern="^1[3-9]\d{9}$">
<span class="form-hint">格式示例:13803780000</span>
</div>
<div class="form-group">
<label for="real-name">*真实姓名:</label>
<input type="text" id="real-name" name="real-name" required pattern="^[\u4e00-\u9fa5]{2,5}$" title="由2-5个中文组成">
<span class="form-hint">由2-5个中文组成</span>
</div>
<div class="form-group">
<label for="province">省份:</label>
<select id="province" name="province">
<option value="">--请选择--</option>
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<!-- other provinces -->
</select>
</div>
<div class="form-group">
<label>*技术方向:</label>
<label><input type="radio" name="tech" value="java"> Java</label>
<label><input type="radio" name="tech" value=".net"> .Net</label>
<label><input type="radio" name="tech" value="php"> PHP</label>
<label><input type="radio" name="tech" value="web"> 网页</label>
<label><input type="radio" name="tech" value="ios"> IOS</label>
<label><input type="radio" name="tech" value="android"> Android</label>
</div>
<div class="form-footer">
<input type="submit" value="注册">
<input type="reset" value="重置">
</div>
</form>
</div>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: url('../img/R-c.png') no-repeat center center fixed;
background-size: cover;
height: 100vh;
margin: 0;
}
.container {
width: 90%;
max-width: 700px;
margin: 50px auto;
padding: 20px;
background: rgba(255, 255, 255, 0.8);
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #f00; /* Darker red for better visibility */
background-color: #ff0; /* Slightly muted yellow */
padding: 10px;
border-radius: 5px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: inline-block;
width: 100px;
font-weight: bold;
vertical-align: middle; /* 确保与单选框对齐 */
}
.form-group input,
.form-group select {
width: calc(100% - 120px);
padding: 8px;
}
.form-group input[type="radio"] {
width: auto; /* 使用默认宽度 */
padding: 0; /* 移除内边距 */
vertical-align: middle; /* 保持与标签对齐 */
}
.form-hint {
display: block;
color: #f00;
}
.form-hint.valid {
color: #0f0;
}
.form-footer {
text-align: center;
}
.form-footer input {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
}
scripts.js
document.addEventListener("DOMContentLoaded", function() {
const form = document.getElementById('registration-form');
form.addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
if (validateForm()) {
this.submit(); // 表单验证成功后提交表单
} else {
alert('请检查表单中的错误!');
}
});
form.addEventListener('blur', function(event) {
validateField(event.target);
}, true);
});
function validateField(field) {
const fieldTypes = {
'real-name': {
pattern: /^[\u4e00-\u9fa5]{2,5}$/,
message: '由2-5个中文组成'
},
'username': {
pattern: /^[a-zA-Z0-9]{3,5}$/,
message: '用户名由3-5个字符组成'
},
'password': {
pattern: /^[a-zA-Z0-9]{8,12}$/,
message: '请输入8-12位密码'
},
'confirm-password': {
pattern: new RegExp(document.getElementById('password').value),
message: '两次密码不一致',
test: (value) => value === document.getElementById('password').value
},
'email': {
pattern: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,
message: '格式示例:xxxxxxxx@163.com'
},
'phone': {
pattern: /^1[3-9]\d{9}$/,
message: '格式示例:13803780000'
}
};
const type = field.id;
const config = fieldTypes[type];
if (!config) return;
const isValid = config.test ? config.test(field.value) : config.pattern.test(field.value);
updateHint(field, isValid, config.message);
}
function updateHint(field, isValid, message) {
const hint = field.nextElementSibling;
if (isValid) {
hint.classList.remove('form-hint');
hint.classList.add('valid');
hint.textContent = '符合要求';
} else {
hint.classList.remove('valid');
hint.classList.add('form-hint');
hint.textContent = message;
}
}
function validateForm() {
const fields = document.querySelectorAll('#registration-form input');
let isValid = true;
fields.forEach(field => {
validateField(field);
if (field.nextElementSibling.classList.contains('form-hint')) {
isValid = false;
}
});
return isValid;
}
效果: