2 Replies Latest reply: Oct 17, 2013 3:35 PM by vincent_deelen RSS

    Validate Date Format

    Gus C

      Apex 3.2

       

      I have a field called P14_START_DATE.

      The display as type is Date Picker (DD.MM.YYYY).

       

      Now if the user uses the pop up calendar, the field is populated with eg 07.12.2013.

       

      I have 2 standard validations, not null and item specified is a valid date.

       

      If the user decides to type in the date, they can use a different format, eg 07/12/2013, which I do not want to happen.

      How can I validate the field, so they get an error if they do not use format dd.mm.yyyy

       

      Gus

        • 1. Re: Validate Date Format
          fac586

          GusC wrote:

           

          If the user decides to type in the date, they can use a different format, eg 07/12/2013, which I do not want to happen.

          How can I validate the field, so they get an error if they do not use format dd.mm.yyyy

          Create a PL/SQL Function Returning Boolean validation for the item, and try to convert the item value to a date using the FX format mask to require exact matching between the character data and the format model.

           

          declare
          
            d date;
          
            invalid_date_format exception;
              pragma exception_init(invalid_date_format, -1861);
          
            invalid_date_length exception;
              pragma exception_init(invalid_date_length, -1862);
          
          begin
          
            d := to_date(:p14_start_date, 'fxdd.mm.yyyy');
          
            return true;
          
          exception
          
            when invalid_date_format or invalid_date_length
            then
              return false;
          
          end;
          
          
          
          • 2. Re: Validate Date Format
            vincent_deelen

            Hi Gus,

             

            I'd prefer a javascript validation for this, since it can check the date format on the fly.

            Check this example:

            http://apex.oracle.com/pls/apex/f?p=vincentdeelen:13

             

            It uses a dynamic action that fires on change of the date field. the javacscript of the dynamic action looks like this:

               

                // regular expression that checks the date format

                regExpr = /^\d{1,2}\.\d{1,2}\.\d{4}$/

               // validate the date field and throw alert if it doesn't match the regular expression

             

             

                if($('#P13_COL2') != '' && !$('#P13_COL2').val().match(regExpr)) {

                  alert('Date must be of type "DD.MM.YYYY"')

                  $('#P13_COL2').focus()

                  return false

                }

             

            Kind regards,

            Vincent