Page 1 of 1

full name regex validation - help

Posted: Mon Jun 27, 2005 4:54 pm
by mushi
I want to create a reg. expression to validate a full name field. The following are examples of name:

Smith, John
O'Neil, Jennie
Anderson, J.A.

Expression: $pattern = "^[A-Za-z][\s]?[A-Za-z,'.]";

There should be no numerical or meta characters allowed. For some reason, it allows number and meta characters. :( . Please help

Posted: Mon Jun 27, 2005 5:00 pm
by Chris Corbyn
You needed a $ at the end or characters can slip in providing the first part of the pattern matches.

This is more slick anyway ;)

Code: Select all

$pattern = "/^[a-z][a-z\']+,\s?[a-z][a-z\'\.]+$/i";
EDIT | You also need to escape that dot "." since that's a meta character for ANY character. "\." is a literal dot.

Posted: Tue Jun 28, 2005 1:43 am
by Syranide
d11wtq wrote:EDIT | You also need to escape that dot "." since that's a meta character for ANY character. "\." is a literal dot.
Neither did he use the $-modifier so he would allow a whole bunch of things.

EDIT: *oops* ... er, the monitor is too small ... ...

Posted: Tue Jun 28, 2005 1:51 am
by Chris Corbyn
Syranide wrote:
d11wtq wrote:EDIT | You also need to escape that dot "." since that's a meta character for ANY character. "\." is a literal dot.
Neither did he use the $-modifier so he would allow a whole bunch of things.
Hmmm... as mentioned in first point above ;)

Posted: Tue Jun 28, 2005 10:43 am
by mushi
Now I get an error: Warning: ereg(): REG_EBRACK
and it also allows any characters.

Edit: it works now :) . Thanks so much!