Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

php

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.

like image 419
Bill Avatar asked Feb 13 '10 20:02

Bill


2 Answers

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

À-ÿ

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

/^([a-zA-ZÀ-ÿ-' ]+)$/
like image 170
Jeremy Knight Avatar answered Sep 16 '22 15:09

Jeremy Knight


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.

like image 20
Andy E Avatar answered Sep 18 '22 15:09

Andy E