Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    PHP Regular Expressions

    For my preg_match I am allowing a-z, A_Z, 0-9 and the following - _ .

    How do I make it so that there cannot be anymore then 1 of the underscore, dash and period in a row??

    so you can do this

    jane.doe.whatever

    but never this

    Jane..doe

    PHP Code:
    if (preg_match('/[^a-z0-9-_\.]/i'$userID)) 

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    We'll wanna double check this but...

    PHP Code:
    if (preg_match('/\.{2,}|-{2,}|_{2,}/'$userID)) echo 'Invalid!' 

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    No that doesn't work. Allows you to put as many as you want.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    239
    Thanks
    1
    Thanked 56 Times in 55 Posts
    You can do it using the following two patterns or one more sophisticated.
    PHP Code:
    $userID "Jane..doe";
    if (
    preg_match('#^[a-z0-9_.-]+$#i'$userID) && !preg_match('#([._-])\1#'$userID))
            echo 
    "valid";
    else
            echo 
    "invalid"

  • Users who have thanked gvre for this post:

    harkly (07-18-2012)

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks! That did it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •