pdxperl has asked for the wisdom of the Perl Monks concerning the following question:

I've got a simple dancer app where users can enter dates that will eventually get stored in MySQL in yyyy-mm-dd format. What's the recommended way of validating user entered dates via a web form? I'd assume forcing them to be entered yyyy-mm-dd might be confusing to some, so I'd like to accept a range of standard formats, ie mm-dd-year, mm/dd/year, etc and then convert to MySQL ISO format. Would you try to handle this in javascript or would you instead use Date:Parse or something similar? I've got a javascript date picker, which can guarantee the correct format, but I hate to force that as the only method available.
  • Comment on Recommended approach for dealing with user-entered dates in webapp

Replies are listed 'Best First'.
Re: Recommended approach for dealing with user-entered dates in webapp
by tobyink (Canon) on Jul 27, 2014 at 06:17 UTC

    Virtually any modern browser has a build-in date picker...

    <input name="myfield" type="date" value="2014-06-27">
      Thanks, Is this summary not accurate then? http://caniuse.com/input-datetime

        For those browsers without support for type="date", the input field falls back to text entry.

        Also, there exists Modernizr.js, which is a useful shim to add support for recent HTML features to older browsers.

Re: Recommended approach for dealing with user-entered dates in webapp
by Your Mother (Archbishop) on Jul 27, 2014 at 06:37 UTC

    tobyink offers the modern HTML(5) way. You also asked about validation. You RFC:MUST not trust user input, Validation is only secure on the server side. JS has no say whatsoever in what a hacker decides to send or an old or non-compliant browser submits. Check all user data. I personally tend to reach for Date::Calc’s check_date for raw validation. Up to you to decide what ranges are acceptable beyond the pure validity of the YYYY, MM, DD given to it.

      Thanks, I was actually starting to use date::simple and just wondered if there was a more modern way.
Re: Recommended approach for dealing with user-entered dates in webapp
by Laurent_R (Canon) on Jul 27, 2014 at 11:09 UTC
    Please be aware that in many (probably most) countries of the world, the most common format for dates is DD/MM/YYYY or DD-MM-YYYY (i.e. with the day in the month before the month). So you'd better make clear to the user what you expect.