SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Pandora Tipem's Avatar
    Join Date
    Feb 2006
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating a Username

    Hi,

    How can I validate a username using preg_match and only allow alphanumerics and underscores and NOTHING else? I don't understand a thing about regex.

    Thanks
    Tipem no fancy sig, yup
    Need Neopets Help? Try Neopets
    fun adoptable eggs

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match('/^[a-z0-9_]+$/i'$username); 

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    or just use \w

    \w = [a-zA-Z0-9_]
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    Pandora Tipem's Avatar
    Join Date
    Feb 2006
    Posts
    450
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Works like a charm.
    Tipem no fancy sig, yup
    Need Neopets Help? Try Neopets
    fun adoptable eggs

  5. #5
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("Content-type: text/plain");
    $str "AbcdefghijklmnopqrstUvwxyz_`~!@#$%^&*()+-={}[]:;\'<>?,.";
    preg_match("/\w+/",$str,$m);
    var_dump($m); 
    Code:
    array(1) {
      [0]=>
      string(27) "AbcdefghijklmnopqrstUvwxyz_"
    }
    \w A single word character - alphanumeric and underscore.
    my mobile portal
    ghiris.ro


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
  •