SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    ǖber abstrakt's Avatar
    Join Date
    Dec 2001
    Location
    Earth
    Posts
    261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match - date format pattern - STRICT

    I think I have finally gone nutty... regular expressions just will not click for me.

    I need a pattern that strictly limits the user to:

    MM/DD/YYYY format

    0or1 0-2 / 0-3 0-9 / 2 0or1 0-9 0-9

    Does that make any sense? I would appreciate some insight on putting this badboy together. Thank you


  2. #2
    ǖber abstrakt's Avatar
    Join Date
    Dec 2001
    Location
    Earth
    Posts
    261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am getting somewhere, but it still allows an invalid date to be entered, for instance:
    18-22-2005

    Help, please.

    PHP Code:
      <?php
      
     $pattern 
    "/([0-1]{1})([0-9]{1})-([0-3]{1})([0-9]{1})-([2]{1})([0-1]{1})([0-9]{2})/";    // 2digSEP2digSEP4dig
                    
                      
    if(preg_match($pattern$_GET['dBegin'])){;
                          echo 
    "valid date<br/>";
                      }else{
                          echo 
    "invalid date<br/>";
                      }
      
      
    ?>


  3. #3
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try

    (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  4. #4
    ǖber abstrakt's Avatar
    Join Date
    Dec 2001
    Location
    Earth
    Posts
    261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Unknown modifier '['
    Thank you for helping though... it's probably something I am goofing up at this point.


  5. #5
    ǖber abstrakt's Avatar
    Join Date
    Dec 2001
    Location
    Earth
    Posts
    261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could someone please post the proper pattern, and explain it in plain english.

    I want the end-user to have to input a valid date in the following format:
    MM-DD-YYYY

    valid meaning no 14-35-200777

    I'd really appreciate it... as it's making me nuts... getting up for a beather might help though. :|


  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abstrakt
    Code:
    Unknown modifier '['
    Thank you for helping though... it's probably something I am goofing up at this point.
    Sorry, forgot brackets. Also ive taken out the . seperator as you don't require them.

    Try this in your $pattern

    PHP Code:
    $pattern "((0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-](19|20)\d\d)"
    Explaination:

    ( (0[1-9]|1[012]) [-] (0[1-9]|[12][0-9]|3[01]) [-] (19|20)\d\d )

    Front and rear bracket just enclose the expression.

    (0[1-9]|1[012]) Zero followed by a One to Nine OR One followed by a Zero One or Two

    [-] must be character -

    (0[1-9]|[12][0-9]|3[01]) Zero followed by a One to Nine OR a One or a Two followed by a Zero to Nine OR a Three followed by a Zero or One

    [-] must be character -

    (19|20)\d\d ) Nineteen OR Twenty followed by two decimal numbers

    Terry
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


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
  •