Log in

View Full Version : Only Letters and Spaces



MrSheen
07-26-2007, 03:37 PM
Hey there, im making a user registration..

Is there a way to make it check that it is only letters and spaces being submitted?

The name cannot contain any special characters.

Let me know

Thanks

djr33
07-26-2007, 06:41 PM
I was working on the same thing, and I got confused.

I don't know regular expressions very well.

Here's what I came up with:

$name = ereg_replace('[^a-zA-Z0-9]','',$name);

That SHOULD be 'replace any instances of not (lowercase, uppercase and numbers) with nothing.

But it doesn't work. Without the ^, it works, but in the opposite way, taking out all letters/numbers.

Note: remove 0-9 if you won't want to allow numbers.

I think someone else should be able to figure this out. I just haven't found an answer yet.

jonnyynnoj
07-26-2007, 06:59 PM
A quick something I came up with, not tested it much, but it worked if the name contained either a number, _, ?, # or ^.


$name = $_POST['name'];

$pattern = "/([0-9_?#^])+/"; // Add more illegal characters here...

$badname = preg_match($pattern, $name); // Will return 0 if no characters are found...

if ($badname!='0'){
// Name contains some illegal characters...
echo 'The name must only contain letters and spaces...';
}
else {
// The name only contains letters and spaces, do whatever...
echo 'Name didn\'t container illgeal characters...';
}

To use just add more characters between []: $pattern = "/([0-9_?#^])+/";

djr33
07-26-2007, 07:42 PM
That's ineffective, though. Even using all of the characters on the keyboard, something else might be entered. You really need a list generated as what CAN be included, not what can't.

Anyway, this is a more extensive character list:
'[!0-9#$%&\'()*+,-./ :;?@\`{|}~^"]'

MrSheen
07-27-2007, 10:32 AM
Thanks for everything guys! :)