12 Replies Latest reply: May 8, 2012 5:15 AM by Adam Cameron. RSS

    Valid Date Checking

    Phinehas1234

      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?

        • 1. Re: Valid Date Checking
          Dan Bracuk Community Member

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

          • 2. Re: Valid Date Checking
            Adam Cameron. Community Member

            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

            • 3. Re: Valid Date Checking
              BKBK MVP

              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>

              • 4. Re: Valid Date Checking
                Dan Bracuk Community Member

                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.

                • 5. Re: Valid Date Checking
                  BKBK MVP

                  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.

                  • 6. Re: Valid Date Checking
                    Adam Cameron. Community Member

                    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

                    • 7. Re: Valid Date Checking
                      Phinehas1234 Community Member

                      Since there are many several pages using three textboxes to form a date, I do not prefer to combine them into one although I understand the this validation and implementation of one textbox is better than three textboxes.

                      • 8. Re: Valid Date Checking
                        Phinehas1234 Community Member

                        To All,

                         

                        Your discussion broaden me more in ColdFusion knowledge.

                         

                        Lastly, I decide to stop the form submission if it detects the invalid date using JavaScript. This is because JavaScript can allow me to combine three textbox value into one and do the date checking.

                        • 9. Re: Valid Date Checking
                          Adam Cameron. Community Member

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

                           

                          --

                          Adam

                          • 10. Re: Valid Date Checking
                            Phinehas1234 Community Member

                            I see the discussion mention <cfcalendar> and <cfinput type="datefield">.

                             

                            I have tried the <cfinput type="datefield">. Is there any method to change the style of the calendar? For example, I want to show weekdays in white background color but show weekends in pink color. And press calendar icon can remove the calendar rather than press "cross" icon (hide the "cross" icon. Set larger text font size and so on.

                            • 11. Re: Valid Date Checking
                              Dan Bracuk Community Member

                              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?

                              • 12. Re: Valid Date Checking
                                Adam Cameron. Community Member

                                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