如何使用preg_match检查表单POST只包含多个字段的字母?

时间:2022-04-09 20:13:33

I have this current code that allows me to check the users first name from a registration form to ensure it only contains letters, spaces and dashes. However how can I enable checking multiple fields (e.g. last name too).

我有这个当前的代码,允许我从注册表单中检查用户的名字,以确保它只包含字母,空格和短划线。但是,如何启用检查多个字段(例如姓氏)。

/* Checks if the first name only includes letters, dashes or spaces */
   if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname']) == 0)
        $errors .="Your name must only include letters, dashes, or spaces.";

I've tried the following but it seems to only check one or the other, not both.

我尝试过以下但似乎只检查一个或另一个,而不是两个。

  if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'], $_POST['lastname']) == 0)

and also:

并且:

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] and $_POST['lastname']) == 0)

Thanks in advance for any suggestions.

在此先感谢您的任何建议。

5 个解决方案

#1


3  

Since both of the fields are going to be validated with the same regex and you don't want to return any specific feedback about which one, if any, fails you can simply concatenate the two strings.

由于两个字段都将使用相同的正则表达式进行验证,并且您不希望返回有关哪一个(如果有)失败的任何特定反馈,您可以简单地连接两个字符串。

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] . $_POST['lastname']) == 0)

#2


1  

You can use preg_grep, which is preg_match for arrays.

您可以使用preg_grep,它是数组的preg_match。

$results = preg_grep('/[^a-zA-Z -]/', $_POST);
if (count($results) > 0) {
   die("Some field has invalid characters");
}

But, as AurimasL has pointed out above, you'd probably want to validate each field individually, so you can give better feed back instead of just a blanket "You screwed up, fix it!" error.

但是,正如AurimasL上面指出的那样,你可能想要单独验证每个字段,这样你就可以提供更好的反馈,而不仅仅是一条毯子“你搞砸了,修复它!”错误。

Of course, nothing says you can't use this preg_grep as a quick/dirty check for ANY errors, then do individual field checks in case there ARE errors.

当然,没有什么说你不能使用这个preg_grep作为任何错误的快速/脏检查,然后在出现ARE错误的情况下进行单独的现场检查。

#3


0  

You can do this kind of thing using preg_match but it is a very dirty thing because you will never know what is the field that don't respect your rules. You can concatenate the strings like the example:

你可以使用preg_match做这种事情,但它是一个非常脏的东西,因为你永远不会知道什么是不尊重你的规则的字段。您可以像示例一样连接字符串:

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] . $_POST['lastname']) == 0)

Anyway I suggest you to check every field individually and not do this kind of things.

无论如何,我建议你单独检查每个领域,而不是做这种事情。

#4


0  

Validate each field

验证每个字段

 if (preg_match("/^[a-zA-Z\s-]+$/i", $_POST['firstname']) == 0) {
   $errors .="Your name must only include letters, dashes, or spaces.";
 }
 if(preg_match("/^[a-zA-Z\s-]+$/i",    $_POST['lastname']) == 0) {
   $errors .="Your lastname must only include letters, dashes, or spaces.";
 }

Or Both (if user firstname and lastname separated by space)

或两者(如果用户名和姓氏用空格分隔)

 if(preg_match("/^([a-zA-Z\s-]+)\s+([a-zA-Z\s-]+)$/i",$_POST['lastname'] . $_POST['lastname']) == 0) {

}

#5


-1  

The solution to your problem is called: copy and pasting.

您的问题的解决方案称为:复制和粘贴。

You need two if checks, or at least two conditions. You cannot pass two parameters to check onto preg_match.

如果检查需要两个,或者至少需要两个条件。您无法传递两个参数来检查preg_match。

That's the notation style I would advise you (even if I get some flak for that):

这是我建议你的符号样式(即使我得到了一些高手):

if ( preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'])
and  preg_match("/^[a-zA-Z -]+$/", $_POST['lastname'])  )
{
    ...
}

It's important to note that the parenthesis have to be closed around each preg_match(...) call. The outer parens for the if() should stand out. If you are new to the language you should be diligent with formatting.

值得注意的是,必须围绕每个preg_match(...)调用关闭括号。 if()的外部应该突出。如果您是该语言的新手,您应该勤于进行格式化。

If you wanted to check for the not matching condition, then preface each preg_match() call with a ! NOT rather.

如果你想检查不匹配的条件,那么在每个preg_match()调用前加上!不是。

#1


3  

Since both of the fields are going to be validated with the same regex and you don't want to return any specific feedback about which one, if any, fails you can simply concatenate the two strings.

由于两个字段都将使用相同的正则表达式进行验证,并且您不希望返回有关哪一个(如果有)失败的任何特定反馈,您可以简单地连接两个字符串。

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] . $_POST['lastname']) == 0)

#2


1  

You can use preg_grep, which is preg_match for arrays.

您可以使用preg_grep,它是数组的preg_match。

$results = preg_grep('/[^a-zA-Z -]/', $_POST);
if (count($results) > 0) {
   die("Some field has invalid characters");
}

But, as AurimasL has pointed out above, you'd probably want to validate each field individually, so you can give better feed back instead of just a blanket "You screwed up, fix it!" error.

但是,正如AurimasL上面指出的那样,你可能想要单独验证每个字段,这样你就可以提供更好的反馈,而不仅仅是一条毯子“你搞砸了,修复它!”错误。

Of course, nothing says you can't use this preg_grep as a quick/dirty check for ANY errors, then do individual field checks in case there ARE errors.

当然,没有什么说你不能使用这个preg_grep作为任何错误的快速/脏检查,然后在出现ARE错误的情况下进行单独的现场检查。

#3


0  

You can do this kind of thing using preg_match but it is a very dirty thing because you will never know what is the field that don't respect your rules. You can concatenate the strings like the example:

你可以使用preg_match做这种事情,但它是一个非常脏的东西,因为你永远不会知道什么是不尊重你的规则的字段。您可以像示例一样连接字符串:

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] . $_POST['lastname']) == 0)

Anyway I suggest you to check every field individually and not do this kind of things.

无论如何,我建议你单独检查每个领域,而不是做这种事情。

#4


0  

Validate each field

验证每个字段

 if (preg_match("/^[a-zA-Z\s-]+$/i", $_POST['firstname']) == 0) {
   $errors .="Your name must only include letters, dashes, or spaces.";
 }
 if(preg_match("/^[a-zA-Z\s-]+$/i",    $_POST['lastname']) == 0) {
   $errors .="Your lastname must only include letters, dashes, or spaces.";
 }

Or Both (if user firstname and lastname separated by space)

或两者(如果用户名和姓氏用空格分隔)

 if(preg_match("/^([a-zA-Z\s-]+)\s+([a-zA-Z\s-]+)$/i",$_POST['lastname'] . $_POST['lastname']) == 0) {

}

#5


-1  

The solution to your problem is called: copy and pasting.

您的问题的解决方案称为:复制和粘贴。

You need two if checks, or at least two conditions. You cannot pass two parameters to check onto preg_match.

如果检查需要两个,或者至少需要两个条件。您无法传递两个参数来检查preg_match。

That's the notation style I would advise you (even if I get some flak for that):

这是我建议你的符号样式(即使我得到了一些高手):

if ( preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'])
and  preg_match("/^[a-zA-Z -]+$/", $_POST['lastname'])  )
{
    ...
}

It's important to note that the parenthesis have to be closed around each preg_match(...) call. The outer parens for the if() should stand out. If you are new to the language you should be diligent with formatting.

值得注意的是,必须围绕每个preg_match(...)调用关闭括号。 if()的外部应该突出。如果您是该语言的新手,您应该勤于进行格式化。

If you wanted to check for the not matching condition, then preface each preg_match() call with a ! NOT rather.

如果你想检查不匹配的条件,那么在每个preg_match()调用前加上!不是。