Please help with regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paulsmith5@hotmail.com

    #1

    Please help with regular expression

    Hi,

    I have the following regular expression to validate a date-time field
    in European or d/m/y h:m:s format.

    ^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2}))
    (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

    I wish to ammend it so that the time or h:m:s part is validated only if
    supplied (i.e. I wish to make the time part optional). At present if a
    time is not supplied the validation fails. I gather that the following
    part of the above expression

    (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$

    validates the time, however I don't know the syntax to make it
    optional. I hope somebody can help.

    Thanks,

    Paul

  • Ivo

    #2
    Re: Please help with regular expression

    <paulsmith5@hot mail.com> wrote >[color=blue]
    > I have the following regular expression to validate a date-time field
    > in European or d/m/y h:m:s format.
    >
    >[/color]
    ^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)
    ?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2
    468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[
    6-9]|[2-9]\d)?\d{2}))[color=blue]
    > (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$
    >
    > I wish to ammend it so that the time or h:m:s part is validated only if
    > supplied (i.e. I wish to make the time part optional). At present if a
    > time is not supplied the validation fails. I gather that the following
    > part of the above expression
    >
    > (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$
    >
    > validates the time, however I don't know the syntax to make it
    > optional. I hope somebody can help.[/color]

    Put brackets () around the part you quoted minus the dollar sign, and put a
    question mark after the closing bracket. Like so:

    ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)?$

    The brackets group the characters so the question mark applies to the whole
    thing. The meaning of the question mark is exactly what you ask for: the
    preceding bit is optional. The dollar sign matches the end of the string so
    applies to the whole regex and should not be inside this group.

    hth
    --
    Ivo




    Comment

    • Dr John Stockton

      #3
      Re: Please help with regular expression

      JRS: In article <1113317408.227 602.111900@f14g 2000cwb.googleg roups.com>
      , dated Tue, 12 Apr 2005 07:50:08, seen in news:comp.lang. javascript,
      paulsmith5@hotm ail.com posted :
      [color=blue]
      >I have the following regular expression to validate a date-time field
      >in European or d/m/y h:m:s format.
      >
      >^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{
      >2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048
      >]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d
      >)?\d{2}))
      >(20|21|22|23 |[0-1]?\d):[0-5]?\d:[0-5]?\d$
      >
      >I wish to ammend it so that the time or h:m:s part is validated only if
      >supplied (i.e. I wish to make the time part optional). At present if a
      >time is not supplied the validation fails. I gather that the following
      >part of the above expression
      >
      >(20|21|22|23 |[0-1]?\d):[0-5]?\d:[0-5]?\d$
      >
      >validates the time, however I don't know the syntax to make it
      >optional. I hope somebody can help.[/color]

      You *could* omit the final $ from the RegExp without *much* loss; then
      you can append " 00:00:00" to the date or date/time to make it a
      date/time or date/time/time, with only the first time being used


      There are much more sensible approaches - well, at least one - to
      validating date and/or time. Read the newsgroup FAQ (Mon, Fri, or WWW
      edition), and see below, and read <URL:http://www.merlyn.demon.co.uk/js-
      date4.htm>.



      Of course, d/m/y is not really European; some use d-m-y, some d/m/y,
      some d.m.y, some y-m-d.

      If you can, however, use YYYY-MM-DD and hh:mm:ss, as per ISO 8601 -
      note, any field numerically 0-9 needs a leading zero.


      I suggest starting with split(" ") or maybe split(/[^0-9:-]+/) to
      break date & time apart, check length to see if there should be a time,
      and validate separately.

      A significant part of the RegExp above is dealing with the 100 & 400
      year Gregorian Rules. That seems unlikely to be needed at present,
      since between 1900-03-01 and 2100-02-28 inclusive the Gregorian Calendar
      gas the same dates as the Julian (13 days earlier); only the 4-year rule
      is of current importance.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • paulsmith5@hotmail.com

        #4
        Re: Please help with regular expression

        Hi Ivo,

        Thanks for the response - its much appreciated. I got it to work.

        Paul

        Comment

        • paulsmith5@hotmail.com

          #5
          Re: Please help with regular expression

          Hi John,

          Thanks for the reply. I know its not perfect but I got it to wok in the
          end. Thanks for your help.

          Paul

          Comment

          • paulsmith5@hotmail.com

            #6
            Re: Please help with regular expression

            Hi John,

            Thanks for the reply. I know its not perfect but I got it to work in
            the end. Thanks for your help.

            Paul

            Comment

            • paulsmith5@hotmail.com

              #7
              Re: Please help with regular expression

              Hi John,

              Thanks for the reply. I know its not perfect but I got it to work in
              the end. Thanks for your help.

              Paul

              Comment

              Working...