SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm trying to get rid of all non-alphanumeric characters from a form input.
    i'm using this piece of code:

    Code:
    if ($uname != ""){
    if(!eregi("^([[:alnum:]_-])$",$uname)) {
    $unameError3 = "<LI>Username can only contain alphanumeric characters</LI>\n";
    $send = "no";
    }
    }
    it works. but it works a little too well.
    it doesn't matter what is submitted, it always resluts in the error.

    anyone have any idea of how to go about it?
    . . . chris

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    if ($uname != ""){
    if(!eregi("^([a-zA-Z_\-])$",$uname)) {
    $unameError3 = "<LI>Username can only contain alphanumeric characters</LI>\n";
    $send = "no";
    }
    }
    It may be a problem with the last hypen (PHP might be interpreting it as "all characters between _ and ], inclusive). Or it might be a problem with [:alnum:].

  3. #3
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    about 5 minutes after i posted the question, i came up with this:

    Code:
    if ($uname != ""){
    if (eregi("([^a-zA-Z0-9])+",$uname)){
    	$unameError3 =  "<LI>User Name can only contain alphanumeric characters</LI>\n";
            $send = "no";
    }
    }
    and it worked.

    that's not to say i didn't appreciate your contribution.
    you seem like you know what you're doing, whereas i got lucky with the code.

    i did try your version, and i'm glad to say that it worked also.

    do you think that i could use it to check more than one form field without coding it several times. along the lines of defining what fields to check and have them checked all in one piece of code?

    i think i would come in handy if you wanted to check a whole form (or most of the form) without having to write it out a bunch of times.

    thanks again for your help..
    . . . chris

  4. #4
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this would work to return 1 if it is a valid username and 0 if it isn't.
    Code:
    function is_valid_username($uname) {
    return eregi("([^a-zA-Z0-9])+",$uname;
    }
    Then,
    is_valid_username("lala#$#$") == 0
    is_valid_username("qslack") == 1

  5. #5
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick correction on qslack's code.
    Code:
    <?
    function is_valid_username($uname)
    {
             return eregi("^[a-z0-9]+$",$uname);
    }
    ?>
    is_valid_username("RoB101") returns 1;
    is_valid_username("R*o*b#") returns null
    is_valid_username("") returns null

    Now I am sure Freddy will come along and correct my code ;)?


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
  •