Results 1 to 5 of 5

Thread: Only Letters and Spaces

  1. #1
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Only Letters and Spaces

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    A quick something I came up with, not tested it much, but it worked if the name contained either a number, _, ?, # or ^.

    PHP Code:
    $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_?#^])+/";

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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#$&#37;&\'()*+,-./ :;?@\`{|}~^"]'
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for everything guys!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •