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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Simple Regex problem :(

    I'm trying to write a regular expression that validates a form entry. It's designed to only allow uppercase alphabetic characters and numbers, it must also be between 2 and 4 characters in length (inclusive).

    I came up with the following.. but it's allowing pretty much everything through, can anyone see where I'm going wrong?

    PHP Code:
    if(preg_match('/^([A-Z0-9]{2,4})$/'$_POST['prefix']) === FALSE){ fail 
    Any help much appreciated.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    See http://in.php.net/preg_match
    Return Values

    preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    PHP Code:
    <?php

    if (!preg_match('/^[A-Z0-9]{2,4}$/'$_POST['prefix'))
    {
      echo 
    "Invalid";
    }

    ?>
    Regards

  • #4
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    excellent, thanks for the help guys. I used to use eregi and I guess I just didn't think to check how preg_match deals with not finding a match.


  •  

    Posting Permissions

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