简体   繁体   中英

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

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 (eg 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.

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

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

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.

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.

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) {

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM