如何使用 preg_match 检查表单 POST 仅包含多个字段中的字母?
我有这个当前代码,允许我从注册表中检查用户的名字,以确保它只包含字母、空格和破折号。但是,如何启用检查多个字段(例如姓氏)。
/* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于这两个字段都将使用相同的正则表达式进行验证,并且您不想返回有关哪个字段(如果有)失败的任何具体反馈,因此您可以简单地连接两个字符串。
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.
您可以使用
preg_grep
,即数组的 preg_match。但是,正如 AurimasL 上面指出的那样,您可能希望单独验证每个字段,这样您就可以提供更好的反馈,而不仅仅是一揽子“你搞砸了,修复它!”错误。
当然,没有什么说你不能使用这个 preg_grep 作为对任何错误的快速/脏检查,然后进行单独的字段检查,以防出现错误。
You can use
preg_grep
, which is preg_match for arrays.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.
你可以使用 preg_match 来做这种事情,但这是一件非常肮脏的事情,因为你永远不知道什么字段不尊重你的规则。您可以像示例一样连接字符串:
无论如何,我建议您单独检查每个字段,不要做这种事情。
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:
Anyway I suggest you to check every field individually and not do this kind of things.
验证每个字段
或两者(如果用户的名字和姓氏以空格分隔)
Validate each field
Or Both (if user firstname and lastname separated by space)
解决您的问题的方法称为:复制和粘贴。
您需要两次
if
检查,或者至少两个条件。您无法传递两个参数来检查preg_match
。这就是我建议您使用的表示法样式(即使我因此受到一些批评):
值得注意的是,每个 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 ontopreg_match
.That's the notation style I would advise you (even if I get some flak for that):
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.