Validating Username

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runway27
    Banned
    New Member
    • Sep 2007
    • 54

    Validating Username

    i have used the following code to validate the username it is working fine
    Code:
    if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
    {
       $error.="User name cannot be blank or has special characters";
    }
    it does not accept UNDERSCORE at the beginning or end however while i was testing with different special characters except for # the validation works fine for all other special characters.

    for example if i enter the user name as = abc#123

    in this case # sign and what comes after # sign is being ignored. so in this case the username is being read as abc ONLY and not abc#123

    this is very strange, how can i still validate # sign and tell the user that # sign is not a valid username like i have been doing with any other special characters like = !@$...........

    please advice.

    thanks.
    Last edited by ronverdonk; May 26 '08, 02:37 PM. Reason: warning: use code tags!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Cannot understand your problem?

    What do you want your regex to accept and not accept?

    Maybe we could help you create a new one.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by runway27
      in this case # sign and what comes after # sign is being ignored. so in this case the username is being read as abc ONLY and not abc#123
      Its not a problem with regexp.

      It seems like you are submitting your username with ajax and not doing encodeURICompon ent() to it before submitting.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Also, if you are submitting your usernames as GET variables, via the URL, the # character and anything that follows it will be parsed as an anchor name by the browser.

        If that is the case, try the urlencode and urldecode functions.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by Atli
          Also, if you are submitting your usernames as GET variables, via the URL, the # character and anything that follows it will be parsed as an anchor name by the browser.

          If that is the case, try the urlencode and urldecode functions.
          But I thought the browser in itself always does urlencoding of the from elements before sending them by GET method (except when using Ajax). Don't you think so?

          Try this example...
          [HTML]<html>
          <form action="page_do esnt_exist.htm" >
          <input name="param" value="">
          <input type="submit" value="Check what's the url">
          </form>
          </html>[/HTML]
          Just type in a value containing # in the field and click button to see what's the url. Isn't it already urlencoded?

          I typed abc#xyz and got page_doesnt_exi st.htm?param=ab c%23xyz in the address bar when clicked on submit.
          # got encoded to %23

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by hsriat
            But I thought the browser in itself always does urlencoding of the from elements before sending them by GET method (except when using Ajax). Don't you think so?
            True. If you submit an actual <form> the browser should take care of this for you.

            I was thinking more along the lines of putting them manually in the URL string.
            Like if you had made a list of users and wanted each name to link to another script, passing along the username in the URL.

            Could have chosen my words more carefully in my previous post. Sorry about that.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Where did you get the idea that this is to do with ajax or URLs?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Oh, because # marks a placement in the page and what comes after that is looked for in a div id.

                I see.

                But why submit it through GET?

                Makes more sense for POST, right?

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by markusn00b
                  Where did you get the idea that this is to do with ajax or URLs?
                  Well, any variable (username in this case) can be posted by 4 ways (according to my knowledge).

                  1. Simple GET - # won't give any trouble as its already urlencoded by browser.
                  2. Simple POST - same here...
                  3. Ajax GET - # could cause a problem, as # in url is for anchors
                  4. Ajax Post - not sure about this one..

                  So I thought it could be case 3.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by markusn00b
                    ......because # marks a placement in the page and what comes after that is looked for in a div id.....
                    Sorry to disagree slightly here.
                    A destination anchor is not just, or only, a div. The destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).

                    Ronald

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Originally posted by Atli
                      True. If you submit an actual <form> the browser should take care of this for you.

                      I was thinking more along the lines of putting them manually in the URL string.
                      Like if you had made a list of users and wanted each name to link to another script, passing along the username in the URL.

                      Could have chosen my words more carefully in my previous post. Sorry about that.
                      Nothing to sorry about dude. :)

                      Yeah, in the case you said, one should do urlencode in PHP before sending the HTML to the browser.

                      Regards

                      Comment

                      • runway27
                        Banned
                        New Member
                        • Sep 2007
                        • 54

                        #12
                        validating username

                        my question is about validation using php. i am validating a username which a user would enter and clicks on a image to find

                        if that username is available. example if a user enters abc#123 php file is reading this value as abc ONLY which i do not

                        want instead the php file should read as abc#123. follow is the sequence of pages. please advice the solution.

                        first page = register.php here a user enters a username and clicks on an image to find out if the username is available or

                        not. using a javascript function of onclick i am reading the value entered in the form in javascript as
                        Code:
                        var useri = document.registrationform.username
                        var valueofuseri = document.registrationform.username.value
                        
                        var recui = /^\s{1,}$/g;
                        
                        if ((useri.value==null) || (useri.value=="") || (useri.length=="") || (useri.value.search(recui))> -1)
                        {
                        alert("Please Enter a User Name")
                        return false
                        }
                        
                        window.open("checkusernamei.php?theusernameis="+valueofuseri, "titleforavailabilityi", "width=680,  height=275, status=1, 
                        
                        scrollbars=1, resizeable=yes");
                        second page = checkusernamei. php = this file uses GET to read what was entered in the form.
                        Code:
                        $username = $_GET["theusernameis"];
                        
                        if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
                        {
                        echo "username is blank or has special characters";
                        }
                        the # sign is being ignored only if the image is clicked in order to check the username, if the user enters abc#123 and

                        clicks the submit button without clicking on the checkuser image button then my php validation for username shows an error

                        message.

                        [code=php]
                        if( $username == "" || !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )
                        { echo "display error message for username"; }
                        [/code]
                        now the problem is with clicking the image only and passing using GET method how can i fix this problem.

                        please advice.

                        thanks.
                        Last edited by Atli; May 27 '08, 06:57 PM. Reason: Added [code] tags.

                        Comment

                        • hsriat
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1653

                          #13
                          [code=javascript]var valueofuseri=encodeURLCompon ent(document.regis trationform.use rname.value);[/code]

                          Dude, this is what I suggested you in the other thread on same problem.
                          I guess you could not locate that thread in the long list of threads.

                          PS: Use [CODE] Tags.

                          Comment

                          • Atli
                            Recognized Expert Expert
                            • Nov 2006
                            • 5062

                            #14
                            Please do not double post your questions.
                            That only serves to cause confusion, waste the time of our fellow members and make us moderators mad!

                            And use [code] tags when posting your code examples.

                            Seeing as you have over 40 posts already, I expect you to know this by now.
                            Take a look at our Posting Guidelines if you haven't already.
                            I will not be happy if I need to repeat this warning!

                            I have merged the other thread into this one.

                            Moderator

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Runway.

                              What is the value of $username when the User inputs 'abc#123'?

                              Comment

                              Working...