用户名和真实姓名不能为空, 如果为空 弹出用户名和真实姓名不能为空 密码不能少于6位,如果密码少于六位 弹出密码格式不正确 两次密码必须一致。 如果两次密码不一致 弹出 密码不一致 手机号长度需要是 11 位 如果手机号长度 不是11位 弹出手机号格式不正确 如果每次输入结束是正确的就在输入框的右侧显示 正确二字 颜色是绿色 否则显示错误二字 颜色是红色 点击提交 全部正确无误 跳转至新页面
时间: 2024-03-29 10:41:44 浏览: 145
以下是一个基于 JavaScript 的表单验证的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<style>
.error {
color: red;
}
.correct {
color: green;
}
</style>
</head>
<body>
<h1>Form Validation</h1>
<form id="myForm" onsubmit="return validateForm()">
<label for="username">Username:</label>
<input type="text" id="username">
<span id="usernameError" class="error"></span>
<br><br>
<label for="fullname">Full Name:</label>
<input type="text" id="fullname">
<span id="fullnameError" class="error"></span>
<br><br>
<label for="password">Password:</label>
<input type="password" id="password">
<span id="passwordError" class="error"></span>
<br><br>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword">
<span id="confirmPasswordError" class="error"></span>
<br><br>
<label for="phoneNumber">Phone Number:</label>
<input type="text" id="phoneNumber">
<span id="phoneNumberError" class="error"></span>
<br><br>
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
var username = document.getElementById("username");
var fullname = document.getElementById("fullname");
var password = document.getElementById("password");
var confirmPassword = document.getElementById("confirmPassword");
var phoneNumber = document.getElementById("phoneNumber");
var usernameError = document.getElementById("usernameError");
var fullnameError = document.getElementById("fullnameError");
var passwordError = document.getElementById("passwordError");
var confirmPasswordError = document.getElementById("confirmPasswordError");
var phoneNumberError = document.getElementById("phoneNumberError");
var valid = true;
// Validate username
if (username.value === "") {
usernameError.innerHTML = "Username cannot be empty.";
valid = false;
} else {
usernameError.innerHTML = "Correct";
usernameError.className = "correct";
}
// Validate fullname
if (fullname.value === "") {
fullnameError.innerHTML = "Full name cannot be empty.";
valid = false;
} else {
fullnameError.innerHTML = "Correct";
fullnameError.className = "correct";
}
// Validate password
if (password.value.length < 6) {
passwordError.innerHTML = "Password must be at least 6 characters long.";
valid = false;
} else {
passwordError.innerHTML = "Correct";
passwordError.className = "correct";
}
// Validate confirmPassword
if (confirmPassword.value !== password.value) {
confirmPasswordError.innerHTML = "Passwords do not match.";
valid = false;
} else {
confirmPasswordError.innerHTML = "Correct";
confirmPasswordError.className = "correct";
}
// Validate phoneNumber
if (phoneNumber.value.length !== 11) {
phoneNumberError.innerHTML = "Phone number must be 11 digits long.";
valid = false;
} else {
phoneNumberError.innerHTML = "Correct";
phoneNumberError.className = "correct";
}
return valid;
}
</script>
</body>
</html>
```
这个代码实现了一个简单的表单验证,当用户点击提交按钮时会对输入的用户名、真实姓名、密码、确认密码、手机号进行验证。如果任意一项不符合要求,就会弹出相应的错误信息。如果输入正确,就会在相应的输入框旁边显示“正确”的字样,并且字体颜色为绿色。最后,如果全部输入都正确,就会跳转到一个新页面。
阅读全文