I need to validate the date format, that can be either 11/11/11 or 11/22/2013, i.e. the year block can be in YY or YYYY and the complete format will either MM/DD/YY or MM/DD/YYYY
I've this code
^(\d{1,2})\/(\d{1,2})\/(\d{4})$
and I've tried
^(\d{1,2})\/(\d{1,2})\/(\d{2}{4})$ // doesn't works, does nothing
and
^(\d{1,2})\/(\d{1,2})\/(\d{2|4})$ // and it returns null every time
PS: I'm applying it with Javascript/jQuery
^(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})$
Both \d{2}{4} and \d{2|4} are not correct regex expression. You have to do two digits and for digits separately and combine then using or: (\d{2}|\d{4})
You could use:
^\d\d?/\d\d?/\d\d(?:\d\d)?$
explanation:
The regular expression:
(?-imsx:^\d\d?/\d\d?/\d\d(?:\d\d)?$)
matches as follows:
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d?                      digits (0-9) (optional (matching the most
                           amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d?                      digits (0-9) (optional (matching the most
                           amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With