Skip navigation
Phinehas1234
Currently Being Moderated

Valid Date Checking

May 6, 2012 5:58 PM

Hi,

 

I have three input boxes to allow user input the date in format dd/mm/yyyy. So, first one is used to input day, second one is month, and third one is year.

 

However, when I use CreateDate(form.yy, form.mm, form.dd) to form a date, it encounters error.

 

After testing, the error occurs because of the invalid date. For example, since default day is 31, if user set month to 4, it will show invalid date. Similar case is 31/02/2012.

 

Is there any checking to check the date valid?

 
Replies
  • Currently Being Moderated
    May 6, 2012 7:12 PM   in reply to Phinehas1234

    Reduce the three checkboxes to one.  Use cfinput validate="date".  On the server side, use isDate() to double check.         

     
    |
    Mark as:
  • Currently Being Moderated
    May 6, 2012 11:44 PM   in reply to Dan Bracuk

    Reduce the three checkboxes to one.  Use cfinput validate="date".  On the server side, use isDate() to double check.         

     

    Bleah. That's a pretty awful suggestion from a UX POV, Dan.

     

    I'd perhaps use a <cfinput type="datefield"> (or - more likely - a JQuery equivalent) if I wanted a unified form control.  I'd not simply have a single input and expect the use to type it all in.

     

    On the server side one needs to revalidate the input.  I dunno off the top of my head what string format a datefield captures the date value as, but it's probably something like mm/dd/yyyy knowing CF (experiment and find out), but I'd pull it apart and recompose it with createDate(), or perhaps parseDateTime() on it.  Either way, that could still error, so it needs to be wrapped in a try/catch, and if the operation fails, one can safely assume the exception arose because what was passed by the form wasn't valid.

     

    CF's isDate() and isValid() date validation functions are rubbish because they will accept anything that could possible be cast as a date as valid.  So like "0" and "1,2" are dates according to CF.  Which is stupid.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 4:06 AM   in reply to Phinehas1234

    I think an even better solution is to combine the suggestions from Dan and Adam.

     

    <cfform>

        <!--- Validate is necessary, for example, in case the user ignores the date picker --->

        <cfinput name="dt" type="datefield" validate="date" message="Please enter a valid date">

        <cfinput type="submit" name="sbmt" value="send">

    </cfform>

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 5:33 AM   in reply to Adam Cameron.

    Regarding, "I'd perhaps use a <cfinput type="datefield">".

     

    Is that the one that renders as a flash movie?  If so, I think it stinks to high heaven and would never use it.  Specifically, any web form that can't be completed using only a keyboard, I consider crapware. 

     

    This forum is a good example.  The tab key should take me out of this textarea to the Add Reply button but it doesn't.  You have to make hand movements that should be optional.

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 6:23 AM   in reply to Dan Bracuk

    Dan Bracuk wrote:

     

    Regarding, "I'd perhaps use a <cfinput type="datefield">".

     

    Is that the one that renders as a flash movie?  If so, I think it stinks to high heaven and would never use it.  Specifically, any web form that can't be completed using only a keyboard, I consider crapware. 

    Type="datefield" is possible in both Flash and HTML forms. For the typist, it offers a text input field besides the date-picker widget.

     

    This forum is a good example.  The tab key should take me out of this textarea to the Add Reply button but it doesn't.  You have to make hand movements that should be optional.

    "This" might not be a textarea.

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 1:16 PM   in reply to Dan Bracuk

    Dan Bracuk wrote:

     

    Regarding, "I'd perhaps use a <cfinput type="datefield">".

     

    Is that the one that renders as a flash movie?

     

    I think you're thinking of <cfcalendar>.  That's just Flash <cfinput> will use HTML on and HTML form.

     

    Other than that, I agree wholeheartedly with your sentiment re Flash-based forms.  It's just unnecessary 95% of the time, and is poorly implemented around 99% of the time, in my experience.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 11:07 PM   in reply to Phinehas1234

    You can't just rely on clientside validation.  You need both.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 5:01 AM   in reply to Adam Cameron.

    Regarding:  "CF's isDate() and isValid() date validation functions are rubbish because they will accept anything that could possible be cast as a date as valid.  So like "0" and "1,2" are dates according to CF.  Which is stupid."

     

    What do you do instead?

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 5:15 AM   in reply to Dan Bracuk

    Regarding:  "CF's isDate() and isValid() date validation functions are rubbish because they will accept anything that could possible be cast as a date as valid.  So like "0" and "1,2" are dates according to CF.  Which is stupid."

     

    What do you do instead?

     

    I disassemble the "date" into component parts; treating it as a "/" (or whatever is being used as the separator in the given situation) -delimited list, and recompose it as a date object, using createDate().  If that doesn't error: it's a date.  If it does error, I send 'em back to the form.

     

    (Obviously the disassembly is only necessary if it's passed as one value, instead of three separate ones)

     

    --
    Adam

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points