Skip to main content
May 19, 2016
Answered

Date Field prevents past dates

  • May 19, 2016
  • 1 reply
  • 604 views

I have crated a form that employees use to move products to another department.  On that field is the expiration date of the product.  I want to crate a field that when the user puts in a previous date (i.e today is 5/19/16 and they put in 5/18/16) the field either turns red or a message box opens up indicating the product has expired.  Thanks

This topic has been closed for replies.
Correct answer George_Johnson

You can use a custom Validate script for the text field, something like the following:

// Validate script for text field

(function () {

    // Do nothing if the field is blank

    if (!event.value) {

        return;

    }

    // Get the current date/time

    var oDate = new Date();

    // Set the time of today's date to midnight

    oDate.setHours(0, 0, 0, 0);

    // Convert field entry to date object

    // Use whatever date format the field is up for

    var oDateEntered = util.scand("m/d/yyyy", event.value);

    if (+oDateEntered < +oDate) {

        // Alert the user

        app.alert("Please enter a date that is not in the past.", 3);

        // Optionally, reject the entry

        event.rc = false;

    }

})();

1 reply

George_JohnsonCorrect answer
Inspiring
May 19, 2016

You can use a custom Validate script for the text field, something like the following:

// Validate script for text field

(function () {

    // Do nothing if the field is blank

    if (!event.value) {

        return;

    }

    // Get the current date/time

    var oDate = new Date();

    // Set the time of today's date to midnight

    oDate.setHours(0, 0, 0, 0);

    // Convert field entry to date object

    // Use whatever date format the field is up for

    var oDateEntered = util.scand("m/d/yyyy", event.value);

    if (+oDateEntered < +oDate) {

        // Alert the user

        app.alert("Please enter a date that is not in the past.", 3);

        // Optionally, reject the entry

        event.rc = false;

    }

})();

May 20, 2016

Thank you so much.  It works great and doesn't get upset with a reset form button.  Thank you so much.