SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match for alphanumeric

    Hi,

    I'm trying to test that $pm_user does not contain anything other than letters or numbers a-z A-Z 0-9

    I put the followiing code but it doesn't ever seem to set $pm_good_login to 0


    $pm_good_for_login = 1
    if (!preg_match('/^[a-zA-Z0-9]/',$pm_user)):
    $pm_good_for_login = 0 ;
    endif ;

    TIA

    Pat

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Pat,

    You need to add a + to the end of your regex pattern, like this:
    PHP Code:
    preg_match('/^[a-zA-Z0-9]+/'$pm_user
    This means 'match the preceding pattern one or more times'.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'd be inclined to use the ctype_alnum() function instead of regex seen as it isn't really necessary in this instance. Its usage is simple: it returns TRUE if all characters are alphanumerical, or FALSE if they're not.

    Quote Originally Posted by fretburner View Post
    Hi Pat,

    You need to add a + to the end of your regex pattern, like this:
    PHP Code:
    preg_match('/^[a-zA-Z0-9]+/'$pm_user
    This means 'match the preceding pattern one or more times'.
    In order for your pattern to be reliable, you'll need to use the $ anchor at the end of the pattern to ensure that the whole string contains only numbers and letters.

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ctype_alnum()

    Much easier , Thanks


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
  •