name validation

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

name validation

Post by yacahuma »

Hello,
I decided is about time I learn some egex. I hope you guys can help me.

I am stuck in the most simple concept

the rule: any uppercase character is will cause the whole string to be invalid.

Code: Select all

if (!preg_match('/[a-z]/', 'xxxxXxxx'))
  echo 'invalid';
else
   echo 'valid';
what am I missing?


When I create rules do I put all the valid characters or the invalid characters?


The first thing I would like is the regular expression for a valid person name. I guess there should be no numbers and no strange characters either(like !@#$%^&*()_+=-[]| ....)


Thank you
PS: I read the tutorials but still stuck.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

For upper case match use

preg_match('/[a-zA-Z]/', 'xxxxXxxx')

or

preg_match('/[a-z]/i', 'xxxxXxxx') - i means case insensitive. You can put both valid or invalid chars (if you do not want to match the char _ you can include it as [^_])

To match a person name you need this pattern #[\w\s]+#i, note that i use # instead of / because when i need to match html tags surrounding the patterns in / needs escape of the tags ;)


P.S. And probably is better to learn regexp than worry about client-side PHP :twisted:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Switch the case of the "a-z" ...
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

still a good idea

Post by yacahuma »

P.S. And probably is better to learn regexp than worry about client-side PHP
I still think it will be a great idea, JAJAJ
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

regular expression pattern library

Post by yacahuma »

Thanks

Code: Select all

if (preg_match('#[^\w\s]#i', 'John íñ'))
  echo 'invalid';
else
   echo 'valid';
Is there a regular expression pattern library?

something like

define('NAME_REGX',''#[^\w\s]#i');

emails, phone, zip code, etc are always the same , why reinvent the wheel.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Possibly, but outside of email addresses, all of those are locale specific.
gr8dane
Forum Newbie
Posts: 19
Joined: Wed Aug 22, 2007 3:12 am

name validation

Post by gr8dane »

To match a person name you need this pattern #[\w\s]+#i
Why would you use this pattern to validate a person's name? First of all, numbers and underscores are not valid parts of a name. Secondly, this pattern would fail perfectly legitimate names such as "O'Reilly", "Mary & Joseph", "Mary Smith-Jones", "Tom Jones, Jr." or 'Tom ("Bud") Jones'.

I'm thinking of using /^[a-zA-Z\&\-\'\ \,\.\"\(\)]+$/ to validate the name in a contact form, but I don't know if there would be any security issues when using the name in mail(). Does anyone know the best pattern to use to validate legitimate names without causing security problems in an email form?
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Re: name validation

Post by mrkite »

gr8dane wrote: I'm thinking of using /^[a-zA-Z\&\-\'\ \,\."\(\)]+$/ to validate the name in a contact form
Why? Who are you to say someone's name is valid or not?
Raganwald wrote: A name is the most personal thing people are going to give you, and here you are “validating” it.
http://weblog.raganwald.com/2007/09/you-suck.html
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

@MrKite: Image

'nuff said.

(I know it was posted already)
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

Kieran Huggins wrote:
'nuff said.

(I know it was posted already)
Escaping names so you can store them correctly is quite different from rejecting names that don't match your preconceptions of what should be allowed in a name.

Every single "solution" posted to this thread will reject someone with an accented character in their name.
Post Reply