views:

39

answers:

2

Hi,

From this questions : http://stackoverflow.com/questions/3073176/javascript-regex-only-letters-allowed

How can I make that expression test for people's name? Currently it doesn't allow spaces between names at all. I need to be able to match something like John Doe

Cheers

+4  A: 
/^[a-zA-Z ]+$/.test( 'John Doe')

Throw any symbols you need in the character class. This is why I said be specific about exactly what you want to validate. This regex will not account for accented characters, if you care about that you'd most likely better go with unicode matching.

meder
and can I do it with javascript? Or will it need to be done server-side?
yoda
It can be done either with JS or server-side.
meder
can someone provide an example?
yoda
an example of what?
meder
@Yoda: You can do it on the client: var UsersName="!John Doe"; if (/^[a-zA-Z ]+$/.test(UsersName)) { alert('Valid name'); } else { alert('Invalid name'); }This won't come out nice in a comment, but it's a working example.
Gert G
that's just the same as @meder answer
yoda
@yoda - what did you need an example of? server-side? im confused.
meder
example of unicode matching client-side
yoda
Never do exclusively client side validation. I can disable javascript or hit your server from a custom crafted form. You don't want me saying my name is `' UNION ALL SELECT user,pass FROM mysql.users`
Jamie Wong
I'm aware of that, and I always do server-side validation .. I just want to do it client-side so that the users don't have to spend unecessary time fulling forms
yoda
A: 

Just add a space

^[\w ]+$

Nor sure if it's your need.

unigg