Limiting characters for an input form

Discussion in 'PHP' started by RyanDoubleyou, Jul 1, 2008.

  1. #1
    Hi, all, I own http://tackypenguin.com.

    I want to make my register form only allow users to use ([a-zA-Z0-9]+) when making a username. How would I go along to check to see if they are only using these?
     
    RyanDoubleyou, Jul 1, 2008 IP
  2. egilfujikawanes

    egilfujikawanes Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi RyanDoubleyou,

    Since you are posting this question under the PHP forum I assume you would like to use PHP to achieve this result.

    You can simply check the post variable for the username like this:
    if (preg_match("/^[a-zA-Z0-9]+$/", trim($_POST['username']))){
    	// Username is correct
    }
    else{
    	// Username is wrong
    }
    PHP:
    Good luck with you script

    Best regards
    Egil Fujikawa Nes
    The Marketing Techie
     
    egilfujikawanes, Jul 1, 2008 IP
  3. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so will it take off all other characters?
     
    RyanDoubleyou, Jul 1, 2008 IP
  4. egilfujikawanes

    egilfujikawanes Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No, this is just a simple if statment to validate the username with your username requierments.

    I don't remember the exact syntax for removing all other carracters from the string, but it should be something like this:

    $username = preg_replace("/[^a-zA-Z0-9\d]/i", "", trim($_POST['username']));
    PHP:
    Good luck with you script

    Best regards
    Egil Fujikawa Nes
    The Marketing Techie
     
    egilfujikawanes, Jul 1, 2008 IP
  5. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #5
    The code posted will check if the username matches the pattern. You can then process it as you wish. The following will remove prohibited characters:

    
    $sStrippedUser = preg_replace('#[^a-z0-9]+#i', '', trim($_POST['username']));
    
    if ( $_POST['username'] == $sStrippedUser )
    {
        echo 'Your username is ' . $_POST['username'];   
    }
    else
    {
        echo 'Prohibited characters have been removed from your username. Your username is ' . $sStrippedUser;    
    }
    
    PHP:
     
    clarky_y2k3, Jul 1, 2008 IP
  6. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    clarky_y2k3, your code didnt work for me. Thanks though. egilfujikawanes, so yours strips all the other characters?
     
    RyanDoubleyou, Jul 1, 2008 IP
  7. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #7
    That's weird, it works fine for me...

    I used the input 'abc123', the result was:
    With the input '$a&b^c*123', the result was:
    What errors do you receive if any? What output do you receive?
     
    clarky_y2k3, Jul 1, 2008 IP
  8. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I have
    $sStrippedUser = preg_replace('#[^a-z0-9]+#i', '', trim($username));
    
    if ( $username != $sStrippedUser )
    {
    $username = $sStrippedUser;
        echo 'Prohibited characters have been removed from your username. Your username is ' . $sStrippedUser;   
    }
    PHP:
    because if the name is correct, i dont want it to say anything to the user. That makes the name blank everytime.
     
    RyanDoubleyou, Jul 1, 2008 IP
  9. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #9
    This works for me:
    
    $sStrippedUser = preg_replace('#[^a-z0-9]+#i', '', trim($username));
    
    if ( $username != $sStrippedUser )
    {
        $username = $sStrippedUser;    
    	echo 'Prohibited characters have been removed from your username. Your username is ' . $username;   
    }
    
    Code (markup):
    Where is $username coming from? I'm assuming you've got register_globals enabled?
     
    clarky_y2k3, Jul 1, 2008 IP
  10. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Okay, I got it to work somehow. Thanks man. By the way, do you know what the error:
    Warning: preg_match(): Compilation failed: nothing to repeat at offset 0 in /home/content/r/y/a/ryanwalters/html/user.php on line 211
    means?

    Like 211 says:

    if(preg_match("/$takenname/i", $Pusername))  {
    
    PHP:
     
    RyanDoubleyou, Jul 1, 2008 IP
  11. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #11
    What's the value of $takenname?

    Also, can you post lines 210-212, please?
     
    clarky_y2k3, Jul 1, 2008 IP
  12. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    $mquery = mysql_query("SELECT * FROM users") or die(mysql_error());
    while($row = mysql_fetch_array($mquery)){
    $takenname = $row['username'];
    if(preg_match("/$takenname/i", $Pusername))  {
    $regerror = "yes";
    }
    }
    PHP:
    $takenname is when it searchs in the database to see if the username requested is already registered.
    $Pusername is the $_POST["username"];
     
    RyanDoubleyou, Jul 1, 2008 IP
  13. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #13
    There's a better way of accomplishing that.

    Query the table to see if there are any users with the same username:
    
    $rQuery = mysql_query('SELECT `id` FROM `users` WHERE `username` = "' . $Pusername . '"'); // replace id if appropriate
    
    PHP:
    Get the number of rows:
    
    $iRows = mysql_num_rows($rQuery);
    
    PHP:
    It always helps to free the result:
    
    mysql_free_result($rQuery);
    
    PHP:
    Determine if a user already exists with that username:
    
    if ( $iRows == 1 )
    {
        // username is taken
    }
    
    PHP:
     
    clarky_y2k3, Jul 1, 2008 IP
  14. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    
    $rQuery = mysql_query('SELECT `id` FROM `users` WHERE `username` = "' . $Pusername . '"');
    $iRows = mysql_num_rows($rQuery);
    $iRows = mysql_num_rows($rQuery);
    if ( $iRows == 1 ) {
    // username is taken
    }
    PHP:
    And will this be seen as a copy if RyAN is in the DB, but ryan is put in the form?
     
    RyanDoubleyou, Jul 1, 2008 IP
  15. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #15
    This should eliminate any case-related issues:
    
    $rQuery = mysql_query('SELECT `id` FROM `users` WHERE LOWER(`username`) = "' . strtolower($Pusername) . '"');
    
    PHP:
     
    clarky_y2k3, Jul 1, 2008 IP
  16. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thats work. Thank you
     
    RyanDoubleyou, Jul 1, 2008 IP