9 Replies Latest reply on Aug 24, 2013 8:17 AM by PaulWebb

    Validate data entered is a date

    PaulWebb

      FMA 12

       

      In my script I show a customer dialoge that asks for a start and end date. Each goes into a global field (xdate_start and xdate_end).

      I then run an IF statement...

      IF[ not isempty(xdate_start) and not isempty(xdate_end)]

       

      I realized that doesn't take into account if the user enters random data. How can I validate that the user enters a date into the field in the format I want? ie. 08/23/2013

       

      Thanks

        • 1. Re: Validate data entered is a date
          erolst

          Simplest way: use global number EDIT: date!! fields for the input; FM will do the validation for you. Maybe add a custom error message. This way your script can focus on non-empty (try Count ( field1 ; field2 ) = 2; credits to LaR) and startdate < enddate.

           

          As for the format you want: FM accepts any valid date format, so in your example 23.8.013 (mainland European format) would work too; that's OK, because it is stored internally a number, anyway, and you can treat it as such. Use the Inspector to specify the desired display format.

          1 of 1 people found this helpful
          • 2. Re: Validate data entered is a date
            PaulWebb

            Thanks erlost.

             

            I updated my IF statement to

            Count ( GLOBALS::xdate_start ; GLOBALS::xdate_end ) = 2

             

            and

             

            GLOBALS::xdate_start < GLOBALS::xdate_end

             

            That seems do have done the trick. I tried the global numbers idea but ran into problems so I set it back to a date.

            • 3. Re: Validate data entered is a date
              erolst

              Paul Webb wrote:

              That seems do have done the trick. I tried the global numbers idea but ran into problems so I set it back to a date.

               

              Hi Paul,

               

              did I write “global number fields”? (yes, I did…) Of course I meant “global date fields” – sorry if I caused confusion. But it seems you did already use them.

              • 4. Re: Validate data entered is a date
                keywords

                Hi Paul,

                 

                have you tried using the IsValid ( ) function? That will show an error for any field content that is not a valid date.

                • 5. Re: Validate data entered is a date
                  PaulWebb

                  Hi keywords...I did. I tried...

                   

                  IF[ IsValid ( not IsEmpty ( xdate_start ) ) and IF[ IsValid ( not IsEmpty ( xdate_end ) )

                   

                  But it let blank entries pass through. I might have left SetErrorCapture ( on ) and it passed right through. I've never used IsValid(). I'll have to test it again.

                   

                  I exported the script to PDF but it doesn't look like there is a way to attach it. I can do screen captures if it would be helpful to see.

                  • 6. Re: Validate data entered is a date
                    erolst

                    Why fuss around with IsValid () if FileMaker does the validation for you?

                     

                    But if you do, then better correctly.

                     

                    This test doesn't work as intended:

                     

                    IF[ IsValid ( not IsEmpty ( xdate_start ) ) and IF[ IsValid ( not IsEmpty ( xdate_end ) )

                     

                    If xdate_start is empty, then Isempty ( xdate_start ) = 1, the negation is which 0; that is a valid number, so both side of the AND are true, which makes the check pass muster.

                    (If the field is not empty, then you get a 1, which is equally valid…)

                     

                    This does:

                     

                    IF[ IsValid ( GetAsDate ( xdate_start ) ) and IF[ IsValid ( GetAsDate ( xdate_end ) )

                     

                    But like I said: why bother doing this “manually” when you can use date fields and let FM do the job?

                    • 7. Re: Validate data entered is a date
                      PaulWebb

                      Thanks for the example erolst. I did not have my global fields set to always validate. They were set to only validate on entry. Once I changed that I only needed to verify that the fields were not blank. For that I used...

                       

                      IF[ not IsEmpty ( xdate_start ) and not IsEmpty ( xdate_end )]

                       

                      The count function you used is a good trick also. Have to keep that in my back pocket.

                      • 8. Re: Validate data entered is a date
                        erolst

                        Paul Webb wrote:

                         

                        Once I changed that I only needed to verify that the fields were not blank.

                         

                        dateEnd < dateStart? Look into my example file, it has a nicely CF'd button, too …

                        • 9. Re: Validate data entered is a date
                          PaulWebb

                          I saw that but I am not using a layout to enter dates. I am using a custom dialoge in my script for the user to enter dates.