Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
mushi
- Forum Newbie
- Posts: 8
- Joined: Mon Jun 27, 2005 1:34 pm
Post
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
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
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.
-
Syranide
- Forum Contributor
- Posts: 281
- Joined: Fri May 20, 2005 3:16 pm
- Location: Sweden
Post
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 ... ...
Last edited by
Syranide on Tue Jun 28, 2005 2:22 am, edited 2 times in total.
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
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

-
mushi
- Forum Newbie
- Posts: 8
- Joined: Mon Jun 27, 2005 1:34 pm
Post
by mushi »
Now I get an error: Warning: ereg(): REG_EBRACK
and it also allows any characters.
Edit: it works now

. Thanks so much!