好的,我在本网站上查到的所有涉及验证的内容都不是我要找的内容。preg_match验证
我想要做的是在firstname和secondname中的值的最小长度和最大长度,这是我目前拥有的代码。
if (isset($_POST['submit'])) {
$errors = array();
if (isset($_POST['firstname'])) {
$fn = $_POST['firstname'];
} else {
$errors[] = "You have not entered a first name";
}
if (isset($_POST['secondname'])) {
$sn = $_POST['secondname'];
} else {
$errors[] = "You have not entered a second name";
}
我只是想知道如何将preg_match应用于那些最小值是4个字母,最大值是15的人吗?
我不知道这件事情做
if(preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname']))
在做这个,我试图做
if (isset($_POST['firstname']) && preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname')) {
但是,这也给了我一个错误:/
谁能给我这是一个解决方案吗?
谢谢!
更新: -
Nvm,我找到了解决办法。我只是做这个
if (isset($_POST['firstname'])) {
if (preg_match('/^[A-Z \'.-]{4,15}$/i', $_POST['firstname'])) {
$fn = $_POST['firstname'];
} else {
$errors[] = "<center> <h3> You must enter between 4 and 15 characters! </h3></center>";
}
} else {
$errors[] = "You have not entered a name";
} 对于名字和secondname两者。 :)
为什么不使用'strlen()' – Michelle 2012-04-12 21:46:18
*“但是这也给了我一个错误:/”* - 什么错误?请将错误消息的文本添加到您的问题中。 – hakre 2012-04-12 21:48:33
@hakre我正在做我的代码作为服务器端(而不是客户端),因为代码是服务器端,当出现错误时,页面变得空白。所以我不能提供它的错误代码,如果它没有出现。 – FizzyBear 2012-04-12 21:50:25