validating username

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sudhakar

    validating username

    i have used the following code to validate the username it is working
    fine

    =============== =============== ===============
    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.
  • Michael Fesser

    #2
    Re: validating username

    ..oO(Sudhakar)
    >i have used the following code to validate the username it is working
    >fine
    >
    >============== =============== =============== =
    >if( $username == "" || !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i",
    >$username) )
    >{
    >$error.="Use r 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
    Ignored by whom? Does $username contain the full name including the '#'?
    Then preg_match() shouldn't have a problem with it.

    Micha

    Comment

    • Curtis

      #3
      Re: validating username

      Sudhakar wrote:
      i have used the following code to validate the username it is working
      fine
      >
      =============== =============== ===============
      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.
      Use \w, it's locale-specific, and includes underscore. It's simply:

      preg_match('/^\w+$/', $string);

      I'm not sure if this is what you meant.

      --
      Curtis

      Comment

      • Michael Fesser

        #4
        Re: validating username

        ..oO(Sudhakar)
        >On May 26, 3:13 pm, Michael Fesser <neti...@gmx.de wrote:
        >>
        >Ignored by whom? Does $username contain the full name including the '#'?
        >Then preg_match() shouldn't have a problem with it.
        >
        >thanks for replying. i have looked at the process and realised that i
        >am using GET method. following is the page sequence.
        >
        >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
        >============== =============== =============== =
        >var useri = document.regist rationform.user name
        >var valueofuseri = document.regist rationform.user name.value
        >
        >var recui = /^\s{1,}$/g;
        >
        >if ((useri.value== null) || (useri.value==" ") || (useri.length== "") ||
        >(useri.value.s earch(recui))-1)
        >{
        >alert("Pleas e Enter a User Name")
        >return false
        >}
        >
        >window.open("c heckusernamei.p hp?theusernamei s="+valueofuser i,
        >"titleforavail abilityi", "width=680, height=275, status=1,
        >
        >scrollbars=1 , resizeable=yes" );
        What happens if there's no JS available or not enabled on the client?
        Is there another server-side check?

        Anyway, your JavaScript causes the problem. If the user name contains a
        "#" like in "foo#bar" for example, you'll end up with a URL like this:

        checkusernamei. php?theusername is=foo#bar

        But the '#' separates a URL from a fragment identifier, so all the
        server will see here is just "checkusernamei .php?theusernam eis=foo".

        If such special chars are allowed in user names, you have to URL-encode
        them. In a normal form submission the browser does this automatically,
        but here you have to do it manually. Maybe there's already a JS function
        available for that, I don't know.

        Micha

        Comment

        • Sudhakar

          #5
          Re: validating username

          On May 28, 4:36 pm, Michael Fesser <neti...@gmx.de wrote:
          .oO(Sudhakar)
          >
          >
          >
          On May 26, 3:13 pm, Michael Fesser <neti...@gmx.de wrote:
          >
          Ignored by whom? Does $username contain the full name including the '#'?
          Then preg_match() shouldn't have a problem with it.
          >
          thanks for replying. i have looked at the process and realised that i
          am using GET method. following is the page sequence.
          >
          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
          =============== =============== ===============
          var useri = document.regist rationform.user name
          var valueofuseri = document.regist rationform.user name.value
          >
          var recui = /^\s{1,}$/g;
          >
          if ((useri.value== null) || (useri.value==" ") || (useri.length== "") ||
          (useri.value.se arch(recui))-1)
          {
          alert("Please Enter a User Name")
          return false
          }
          >
          window.open("ch eckusernamei.ph p?theusernameis ="+valueofuseri ,
          "titleforavaila bilityi", "width=680, height=275, status=1,
          >
          scrollbars=1, resizeable=yes" );
          >
          What happens if there's no JS available or not enabled on the client?
          Is there another server-side check?
          >
          Anyway, your JavaScript causes the problem. If the user name contains a
          "#" like in "foo#bar" for example, you'll end up with a URL like this:
          >
          checkusernamei. php?theusername is=foo#bar
          >
          But the '#' separates a URL from a fragment identifier, so all the
          server will see here is just "checkusernamei .php?theusernam eis=foo".
          >
          If such special chars are allowed in user names, you have to URL-encode
          them. In a normal form submission the browser does this automatically,
          but here you have to do it manually. Maybe there's already a JS function
          available for that, I don't know.
          >
          Micha
          thanks for replying. javascript is able to read with the # sign, so
          should i use url-encode in javascript or in php. please advice.

          Comment

          • AnrDaemon

            #6
            Re: validating username

            Greetings, Sudhakar.
            In reply to Your message dated Thursday, May 29, 2008, 23:55:03,
            >Ignored by whom? Does $username contain the full name including the '#'?
            >Then preg_match() shouldn't have a problem with it.
            >>
            >thanks for replying. i have looked at the process and realised that i
            >am using GET method. following is the page sequence.
            >>
            >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
            >============== =============== =============== =
            >var useri = document.regist rationform.user name
            >var valueofuseri = document.regist rationform.user name.value
            >>
            >var recui = /^\s{1,}$/g;
            >>
            >if ((useri.value== null) || (useri.value==" ") || (useri.length== "") ||
            >(useri.value.s earch(recui))-1)
            >{
            >alert("Pleas e Enter a User Name")
            >return false
            >}
            >>
            >window.open("c heckusernamei.p hp?theusernamei s="+valueofuser i,
            >"titleforavail abilityi", "width=680, height=275, status=1,
            >>
            >scrollbars=1 , resizeable=yes" );
            >>
            >What happens if there's no JS available or not enabled on the client?
            >Is there another server-side check?
            >>
            >Anyway, your JavaScript causes the problem. If the user name contains a
            >"#" like in "foo#bar" for example, you'll end up with a URL like this:
            >>
            >checkusernamei .php?theusernam eis=foo#bar
            >>
            >But the '#' separates a URL from a fragment identifier, so all the
            >server will see here is just "checkusernamei .php?theusernam eis=foo".
            >>
            >If such special chars are allowed in user names, you have to URL-encode
            >them. In a normal form submission the browser does this automatically,
            >but here you have to do it manually. Maybe there's already a JS function
            >available for that, I don't know.
            thanks for replying. javascript is able to read with the # sign, so
            should i use url-encode in javascript or in php. please advice.
            You'd better use POST method instead of GET, but if GET is absolutely
            required, encode data before sending to server.


            --
            Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

            Comment

            Working...