Regular Expression Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wwolfson
    New Member
    • Jun 2007
    • 13

    Regular Expression Problem

    Hiya all,

    I'm trying to validate a date in php to be in the form 12-Sep-2006 for example. However, for some reason the following regex wont work.
    [CODE=php]
    function validate_date($ date)
    {
    if (ereg("^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa][Pp] [Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO][Cc] |[Nn][oO][Vv]|[Dd][Ee][Cc])-(19|20)\d\d$", $date))
    {
    return true;
    }
    [/CODE]

    However, in javascript the following code does work:
    [CODE=javascript]
    if (date.match(/([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][bB]|[Mm][Aa][Rr]|[Aa] [Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][u]l|[aA][Uu][gG]|[Ss][eE][pP]|[oO] [Cc]|[Nn][oO][Vv]|[Dd][Ee][Cc])-(19|20)\d\d/))
    [/CODE]

    Can someone please explain this to me and tell me how to go about getting the php regex to behave properly?
    Thank you, William.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, William.

    Instead of using a regular expression, try using strtotime():
    [code=php]
    function makeSafe_date($ date)
    {
    return date('d-M-Y', strtotime($date ));
    }
    [/code]

    Comment

    • Weisbartb
      New Member
      • Aug 2007
      • 36

      #3
      Another thing is that you should be using preg_match() not ereg().

      Comment

      • wwolfson
        New Member
        • Jun 2007
        • 13

        #4
        Sorry, I have tried using preg_match() but it makes no difference and I do not see how strtodate will help me in any way.

        I am using this as a validator to keep dates in the form 13-Sep-2007.

        Thanks.

        Comment

        • azang
          New Member
          • Sep 2007
          • 3

          #5
          I think the first section went wrong. i.e. for the DAY part. try this:

          [PHP](0[1-9]|[12][0-9]|3[01])[/PHP]

          [LINK TO BLOG REMOVED]- Moderator
          Last edited by ak1dnar; Sep 15 '07, 11:51 AM. Reason: Personal Web Site Link removed : Not allowed in Technical Forums

          Comment

          • wwolfson
            New Member
            • Jun 2007
            • 13

            #6
            still no luck, any more ideas anyone?
            sorry.
            william.

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, William.

              strtotime() can turn *any* valid date string into a Unix timestamp. date() takes a Unix timestamp and turns it into a formatted date.

              As long as strtotime() doesn't return false, you know that the User submitted a valid date/time string. You can then use date() to force the date into a format of your choosing.

              A slightly better version of makeSafe_date() would look like this:
              [code=php]
              function validateDate($s tr)
              {
              $test = strtotime($str) ;

              if( ! $test )
              {
              return false;
              }

              return date('d-M-Y', $test);
              }
              [/code]

              Then all you have to do is make sure validateDate() doesn't return false.

              Comment

              • ronnil
                Recognized Expert New Member
                • Jun 2007
                • 134

                #8
                Originally posted by pbmods
                Heya, William.

                strtotime() can turn *any* valid date string into a Unix timestamp. date() takes a Unix timestamp and turns it into a formatted date.

                As long as strtotime() doesn't return false, you know that the User submitted a valid date/time string. You can then use date() to force the date into a format of your choosing.

                A slightly better version of makeSafe_date() would look like this:
                [code=php]
                function validateDate($s tr)
                {
                $test = strtotime($str) ;

                if( ! $test )
                {
                return false;
                }

                return date('d-M-Y', $test);
                }
                [/code]

                Then all you have to do is make sure validateDate() doesn't return false.
                There's a problem though.... passing 01-02-2007, there's no way of telling if the user meant 1st of February or 2nd of January. It's mostly a problem on international sites i guess. Personally i write how the user should input the date, and trust they are not complete morons (i ofc check that it is a valid date of some sort)

                Comment

                • Weisbartb
                  New Member
                  • Aug 2007
                  • 36

                  #9
                  You can always convert it to a unix_time stamp validate that that time stamp doesn't equal 0 (false return) then convert it into whatever date you want.

                  Comment

                  • wwolfson
                    New Member
                    • Jun 2007
                    • 13

                    #10
                    But will this work if the form is

                    02-Sep-2003
                    i.e. the month in words and not numbers

                    ??

                    Thanks.

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, William.

                      Originally posted by wwolfson
                      But will this work if the form is

                      02-Sep-2003
                      i.e. the month in words and not numbers
                      You betcha.

                      Comment

                      • Weisbartb
                        New Member
                        • Aug 2007
                        • 36

                        #12
                        strtotime() will convert almost every readable and known time/date input.

                        Comment

                        • wwolfson
                          New Member
                          • Jun 2007
                          • 13

                          #13
                          I'm really sorry, this is starting to annoy me now.
                          Still not working!
                          please could someone check this out and tell me wats wrong:

                          [CODE=php]
                          function validate_date($ date)
                          {
                          $test = strtotime($date );
                          if( !$test )
                          {
                          return false;
                          }
                          date('d-M-Y', $test);
                          }
                          [/CODE]

                          Code:
                          $date = $_POST['date'];
                          [CODE="php"]
                          if (validate_event ($event) == true &&
                          validate_locati on($location) == true &&
                          validate_date($ date) != false &&
                          validate_time($ time) == true)
                          [/CODE]

                          Wat is wrong here?? :'(

                          Thanks, William.

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Heya, William.

                            Compare this:
                            Originally posted by pbmods
                            [code=php]function validateDate($s tr)
                            {
                            $test = strtotime($str) ;

                            if( ! $test )
                            {
                            return false;
                            }

                            return date('d-M-Y', $test);
                            }[/code]
                            with this:
                            Originally posted by wwolfson
                            [CODE=php]
                            function validate_date($ date)
                            {
                            $test = strtotime($date );
                            if( !$test )
                            {
                            return false;
                            }
                            date('d-M-Y', $test);
                            }
                            [/CODE]
                            Something's missing....

                            Comment

                            Working...