php 写一个有正则匹配的注册页面
时间: 2024-02-01 12:15:03 浏览: 25
以下是一个简单的 PHP 注册页面,使用正则表达式验证输入的用户名和密码是否符合要求:
```php
<!DOCTYPE html>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<h1>注册</h1>
<form method="post" action="">
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="注册">
</form>
<?php
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// 验证用户名和密码
if(preg_match('/^[a-zA-Z0-9_]{6,16}$/', $username) && preg_match('/^[a-zA-Z0-9_]{6,16}$/', $password)) {
echo "注册成功!";
} else {
echo "用户名和密码必须由6到16个字母、数字或下划线组成!";
}
}
?>
</body>
</html>
```
在上面的代码中,使用了 preg_match() 函数来验证用户名和密码是否符合要求。正则表达式 `/^[a-zA-Z0-9_]{6,16}$/` 指定了用户名和密码必须由6到16个字母、数字或下划线组成。如果符合要求,则显示“注册成功!”;否则,显示“用户名和密码必须由6到16个字母、数字或下划线组成!”。