validate username

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jorntk@yahoo.com

    #1

    validate username

    Hi,

    Can anyone tell me how use regular expression to validate a username
    with alpha numeric and special character but not space?

    Thanks and Regards,

    Jorn
  • Garp

    #2
    Re: validate username

    <jorntk@yahoo.c om> wrote in message
    news:e9da850b.0 404030122.74fe2 318@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > Can anyone tell me how use regular expression to validate a username
    > with alpha numeric and special character but not space?
    >
    > Thanks and Regards,
    >
    > Jorn[/color]

    Like this?

    <?php
    $test=array("te st me", "testme", "test_me", "test89+");
    foreach($test as $x) {
    if(preg_match('/^[A-Za-z0-9_]*$/',$x))
    print "$x was OK.\n";
    else
    print "$x wasn't OK.\n"
    }
    ?>

    HTH
    Garp


    Comment

    • Andy Hassall

      #3
      Re: validate username

      On Sat, 03 Apr 2004 09:33:01 GMT, "Garp" <garp7@no7.blue yonder.co.uk> wrote:
      [color=blue]
      > if(preg_match('/^[A-Za-z0-9_]*$/',$x))[/color]

      if(preg_match('/^\w+$/',$x))

      Does the same thing except shorter, and doesn't accept empty usernames.

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

      Comment

      • jorntk@yahoo.com

        #4
        Re: validate username

        > if(preg_match('/^\w+$/',$x))

        how if i want to match username with space?

        thanks & regards

        Jorn

        Comment

        • Ian.H

          #5
          Re: validate username

          On Mon, 05 Apr 2004 04:27:59 -0700, jorntk@yahoo.co m wrote:
          [color=blue][color=green]
          >> if(preg_match('/^\w+$/',$x))[/color]
          >
          > how if i want to match username with space?
          >
          > thanks & regards
          >
          > Jorn[/color]


          if (preg_match('/^(\w|\s)+$/', $x))


          '\s' defines a space. The (...|...) will select either (\w or \s) so you
          cover both.


          HTH.



          Regards,

          Ian

          --
          Ian.H
          digiServ Network
          London, UK


          Comment

          • Andy Hassall

            #6
            Re: validate username

            On 5 Apr 2004 04:27:59 -0700, jorntk@yahoo.co m (jorntk@yahoo.c om) wrote:
            [color=blue][color=green]
            >> if(preg_match('/^\w+$/',$x))[/color]
            >
            >how if i want to match username with space?[/color]

            /^[\w ]+$/

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

            Comment

            Working...