Appearance
9.4 表单验证
表单验证是确保用户输入数据合法性和安全性的重要步骤,包括非空验证、格式验证和长度验证等。
验证类型
1. 非空验证
确保用户填写了必填字段。
2. 格式验证
确保用户输入的数据符合特定格式(如邮箱、手机号、身份证号等)。
3. 长度验证
确保用户输入的数据长度在合理范围内。
4. 范围验证
确保用户输入的数值在指定范围内。
5. 一致性验证
确保两次输入的密码等数据一致。
示例代码
基本表单验证
php
<?php
// form_validation.php
$errors = [];
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 非空验证
if (empty($_POST['username'])) {
$errors[] = '用户名不能为空';
}
if (empty($_POST['email'])) {
$errors[] = '邮箱不能为空';
} else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = '邮箱格式不正确';
}
if (empty($_POST['password'])) {
$errors[] = '密码不能为空';
} else if (strlen($_POST['password']) < 6) {
$errors[] = '密码长度不能少于6位';
}
if (empty($_POST['confirm_password'])) {
$errors[] = '确认密码不能为空';
} else if ($_POST['password'] !== $_POST['confirm_password']) {
$errors[] = '两次输入的密码不一致';
}
// 年龄范围验证
if (!empty($_POST['age'])) {
$age = (int)$_POST['age'];
if ($age < 18 || $age > 100) {
$errors[] = '年龄必须在18-100之间';
}
}
// 如果没有错误,处理表单数据
if (empty($errors)) {
$success = true;
// 这里可以添加数据库操作等代码
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>表单验证示例</title>
<style>
.error { color: red; }
.success { color: green; }
input { margin: 5px 0; }
</style>
</head>
<body>
<h2>注册表单</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success">
注册成功!
</div>
<?php endif; ?>
<form action="form_validation.php" method="post">
<label>用户名: <input type="text" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"></label><br>
<label>邮箱: <input type="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>"></label><br>
<label>密码: <input type="password" name="password"></label><br>
<label>确认密码: <input type="password" name="confirm_password"></label><br>
<label>年龄: <input type="number" name="age" value="<?php echo isset($_POST['age']) ? htmlspecialchars($_POST['age']) : ''; ?>"></label><br>
<input type="submit" value="注册">
</form>
</body>
</html>使用正则表达式验证
php
<?php
// regex_validation.php
$errors = [];
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 手机号验证
if (empty($_POST['phone'])) {
$errors[] = '手机号不能为空';
} else if (!preg_match('/^1[3-9]\d{9}$/', $_POST['phone'])) {
$errors[] = '手机号格式不正确';
}
// 身份证号验证
if (empty($_POST['id_card'])) {
$errors[] = '身份证号不能为空';
} else if (!preg_match('/^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/', $_POST['id_card'])) {
$errors[] = '身份证号格式不正确';
}
// URL验证
if (!empty($_POST['website'])) {
if (!filter_var($_POST['website'], FILTER_VALIDATE_URL)) {
$errors[] = '网站地址格式不正确';
}
}
if (empty($errors)) {
$success = true;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>正则表达式验证示例</title>
<style>
.error { color: red; }
.success { color: green; }
</style>
</head>
<body>
<h2>信息表单</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success">
提交成功!
</div>
<?php endif; ?>
<form action="regex_validation.php" method="post">
<label>手机号: <input type="text" name="phone" value="<?php echo isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : ''; ?>"></label><br>
<label>身份证号: <input type="text" name="id_card" value="<?php echo isset($_POST['id_card']) ? htmlspecialchars($_POST['id_card']) : ''; ?>"></label><br>
<label>个人网站: <input type="text" name="website" value="<?php echo isset($_POST['website']) ? htmlspecialchars($_POST['website']) : ''; ?>"></label><br>
<input type="submit" value="提交">
</form>
</body>
</html>注意事项
- 服务器端验证:始终在服务器端进行验证,客户端验证只是为了提高用户体验
- 数据过滤:使用
htmlspecialchars()等函数过滤用户输入,防止 XSS 攻击 - 错误提示:提供清晰、具体的错误提示,帮助用户正确填写表单
- 数据类型:根据需要进行适当的类型转换
- 安全性:对于密码等敏感信息,应使用加密存储
练习
- 创建一个包含多种验证规则的表单
- 实现服务器端验证并显示错误信息
- 测试各种边界情况和错误输入
