用PHP创建一个用于填写个人信息的表单,要求使用HTML 5提供的新类型input元素和新增属性对用户输入的用户名、密码、出生日期、 身份证号码、手机号码、电子邮件地址、身高、体重等进行验证。
时间: 2023-09-14 09:04:27 浏览: 55
好的,以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>个人信息表单</title>
<meta charset="UTF-8">
</head>
<body>
<h2>个人信息表单</h2>
<form action="submit.php" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required minlength="6" maxlength="12" pattern="^[a-zA-Z][a-zA-Z0-9_]{5,11}$"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required minlength="8" maxlength="16" pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#\$%\^&\*])(?!.*\s).*$"><br>
<label for="birthdate">出生日期:</label>
<input type="date" id="birthdate" name="birthdate" required max="<?php echo date('Y-m-d', strtotime('-18 years')); ?>"><br>
<label for="idcard">身份证号码:</label>
<input type="text" id="idcard" name="idcard" required pattern="^\d{17}[\dXx]$"><br>
<label for="phone">手机号码:</label>
<input type="tel" id="phone" name="phone" required pattern="^1[3456789]\d{9}$"><br>
<label for="email">电子邮件地址:</label>
<input type="email" id="email" name="email" required><br>
<label for="height">身高:</label>
<input type="number" id="height" name="height" min="100" max="220" step="0.01" required><br>
<label for="weight">体重:</label>
<input type="number" id="weight" name="weight" min="30" max="200" step="0.01" required><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
上面的代码中,我们使用了 HTML5 提供的新类型 input 元素和新增属性,包括:
- `type="text"`:用于输入文本内容的文本框。
- `type="password"`:用于输入密码的文本框,输入内容会被隐藏。
- `type="date"`:用于输入日期的文本框,支持日期选择器。
- `type="tel"`:用于输入电话号码的文本框,支持电话号码格式验证。
- `type="email"`:用于输入邮箱地址的文本框,支持邮箱地址格式验证。
- `type="number"`:用于输入数字的文本框,支持数字格式验证。
- `required`:表示该字段为必填项。
- `minlength` 和 `maxlength`:表示该字段的最小和最大长度。
- `pattern`:用于自定义正则表达式验证该字段的格式。
- `min` 和 `max`:用于设置该字段的最小和最大值。
- `step`:用于设置该字段的步长。
在表单的提交页面 submit.php 中,可以通过 `$_POST` 超全局数组来获取表单中用户输入的值。例如:
```php
$username = $_POST['username'];
$password = $_POST['password'];
$birthdate = $_POST['birthdate'];
$idcard = $_POST['idcard'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$height = $_POST['height'];
$weight = $_POST['weight'];
```
需要注意的是,用户输入的数据都是不可信的,需要进行安全过滤和验证,以防止恶意攻击和注入攻击。