php 尝试验证名称字段以确保它全是字母字符或连字符或撇号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2258922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:45:23  来源:igfitidea点击:

Trying to validate a name field to be sure it is all alpha characters or a hyphen or apostrophe

phpregex

提问by Bill

if(!preg_match("/[a-zA-Z'-]/",$First)) { die ("invalid first name");}

the above only flags input as invalid when the field is all numeric. combinations of letters and numbers pass ok. Some help here for a newby please. thanks.

当字段全部为数字时,上述仅将输入标记为无效。字母和数字的组合通过。请在这里为新手提供一些帮助。谢谢。

回答by Andy E

Try:

尝试:

if(!preg_match("/^[a-zA-Z'-]+$/",$First)) { die ("invalid first name");} 

The ^matches the beginning of the string, the $matches the end of the string and +after a character group means "one or more" matching characters.

所述^匹配的字符串的开头,所述$字符串的末尾匹配和+之后的字符组的意思是“一个或多个”匹配的字符。

回答by cletus

You're only matching a single character. You need to add a wildcard like +(1 or more). Also you're matching anywhere in the string. You need to add anchors (^for start of string, $for end of string) to make sure there are no invalid characters.

你只匹配一个字符。您需要添加一个通配符,如+(1 个或多个)。你也匹配字符串中的任何地方。您需要添加锚点(^用于字符串的开头,$用于字符串的结尾)以确保没有无效字符。

if (!preg_match("/^[a-zA-Z'-]+$/", $First)) { die ("invalid first name");}

Alternatively you can look for a non-matching character:

或者,您可以查找不匹配的字符:

if (preg_match("/[^a-zA-Z'-]/", $First)) { die ("invalid first name");}

The ^inside the square brackets here means "any character that is NOT one of these".

^方括号内这里的意思是“任何一个不是其中之一。”

回答by John Knoeller

In this case better to look for invalid characters than to try and match all of the characters, once a single invalid character appears, the search can quit and return failure. This is more efficient than always scanning the whole string.

在这种情况下,最好查找无效字符而不是尝试匹配所有字符,一旦出现单个无效字符,搜索就会退出并返回失败。这比总是扫描整个字符串更有效。

if (preg_match("/[^A-Za-z'-]/", $First)) { die ("invalid first name"); }

the ^ inside the set [] makes it match everything not in the set. and since the string is invalid if we have even one invalid character, there is no need for the set to have a repetition operator.

集合 [] 中的 ^ 使其匹配集合中没有的所有内容。并且由于即使我们有一个无效字符,字符串也是无效的,因此集合不需要重复运算符。

Even better would be a more helpful error message

更好的是更有用的错误消息

if (preg_match("/[^A-Za-z'-]/", $First, $Inv)) { die ("{$Inv[0]} not allowed in first name"); }

回答by Jeremy Knight

Adding accented characters is probably smart too, so that "Pierre Bézier" can fill out your form. Adding:

添加重音字符可能也很聪明,这样“Pierre Bézier”就可以填写您的表格。添加:

à-?

.. to your regex will do that. Something like this, with everything included:

.. 到你的正则表达式会做到这一点。像这样的东西,包括所有内容:

/^([a-zA-Zà-?-' ]+)$/

回答by STEEL

You can try this one, incase you want FULL NAME e.g. "JOHN RAMBO"

你可以试试这个,以防你想要全名,例如“JOHN RAMBO”

While the HTML FORM.PHP

而 HTML FORM.PHP

<div class="error"><?php echo $error['name']; ?></div>
<input name="name" type="text" value="<?php echo $input['name']; ?>" class="inputf">

PHP CONTACT PAGE

PHP 联系页面

if(!preg_match("/^[a-zA-Z'-]/", $input['name']))
{
    // name is in-valid
    $error['name'] = 'Invalid Name';
    include('views/form.php');
}

回答by Gozie

Here's an solution I was able to come up with. Should be able to handle your validation issues for names that have apostrophes, hypens and spaces.

这是我能够想出的解决方案。应该能够处理包含撇号、连字符和空格的名称的验证问题。

 if(!preg_match('/^([a-zA-Z]+[\'-]?[a-zA-Z]+[ ]?)+$/', $string))

回答by cjohansson

If you use if (!preg_match("/^[a-zA-Z'-]+$/", $firstName)) {it will also match if an error occured, like a invalid regexp, because the !matches for both the return values FALSEand 0.

如果您使用if (!preg_match("/^[a-zA-Z'-]+$/", $firstName)) {它也会在发生错误时匹配,例如无效的正则表达式,因为!返回值FALSE0.

From the PHP Manual: preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

PHP 手册preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

So the best way of making sure that the name is valid by your requirements is:

因此,确保名称符合您的要求的最佳方法是:

if (preg_match("/^[a-zA-Z'-]+$/", $firstName) === 1) {
    die('Valid name');
} else {
    die('Invalid name or error');
}