How can I check a form POST only contains letters on multiple fields using preg_match?

爷,独闯天下 提交于 2019-12-24 07:00:25

问题


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.


回答1:


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:


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.




回答3:


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.




回答4:


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:


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.



来源:https://stackoverflow.com/questions/7828684/how-can-i-check-a-form-post-only-contains-letters-on-multiple-fields-using-preg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!