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

发布于 2024-12-11 04:22:44 字数 592 浏览 0 评论 0原文

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

/* 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.";

我已经尝试过以下操作,但似乎只检查其中之一,而不是两者。

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

另外:

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

提前感谢您的任何建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

并安 2024-12-18 04:22:44

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

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

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)
烟─花易冷 2024-12-18 04:22:44

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

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

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

当然,没有什么说你不能使用这个 preg_grep 作为对任何错误的快速/脏检查,然后进行单独的字段检查,以防出现错误。

You can use preg_grep, which is preg_match for arrays.

$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.

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.

南风几经秋 2024-12-18 04:22:44

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

if(preg_match("/^[a-zA-Z -]+$/", $_POST['firstname'] . $_POST['lastname']) == 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:

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.

ぃ双果 2024-12-18 04:22:44

验证每个字段

 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.";
 }

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

 if(preg_match("/^([a-zA-Z\s-]+)\s+([a-zA-Z\s-]+)$/i",$_POST['lastname'] . $_POST['lastname']) == 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) {

}
云雾 2024-12-18 04:22:44

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

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

这就是我建议您使用的表示法样式(即使我因此受到一些批评):

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

值得注意的是,每个 preg_match(...) 调用都必须用括号括起来。 if() 的外括号应该突出。如果您是该语言的新手,您应该勤于格式化。

如果您想检查 not 匹配条件,请在每个 preg_match() 调用前加上 ! NOT。

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文