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

php, regex

Solution

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.

Problem

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

Original source