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


Question

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.

1
11
2/13/2010 8:35:43 PM

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.

19
2/13/2010 8:29:38 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon