Page 1 of 1

name validation

Posted: Tue Aug 21, 2007 1:55 pm
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.

Posted: Tue Aug 21, 2007 2:01 pm
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:

Posted: Tue Aug 21, 2007 2:03 pm
by feyd
Switch the case of the "a-z" ...

still a good idea

Posted: Tue Aug 21, 2007 2:07 pm
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

regular expression pattern library

Posted: Tue Aug 21, 2007 2:27 pm
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.

Posted: Tue Aug 21, 2007 2:35 pm
by feyd
Possibly, but outside of email addresses, all of those are locale specific.

name validation

Posted: Sun Oct 21, 2007 10:27 pm
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?

Re: name validation

Posted: Mon Oct 22, 2007 3:11 am
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

Posted: Mon Oct 22, 2007 3:50 am
by Kieran Huggins
@MrKite: Image

'nuff said.

(I know it was posted already)

Posted: Mon Oct 22, 2007 3:30 pm
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.