Skip to main content
Participant
November 11, 2023
Question

Auto populate the / / if you enter a date like 05161980 and show 05/16/1980

  • November 11, 2023
  • 1 reply
  • 238 views

I had previously found a script that allowed that only to find that if the input day was from 10-19 it would throw an error. So if you entered 050580 it would populate 05/05/1980 however if you entered 051680 it would give populate 01/05/1680 but if I entered 05161980 it would give error.  I will post the scrip here in hopes I can get help with whats wrong or whats the correct way to do it, thanks in advanced!

 

// adding slashes
var newDate = event.value;
// validate date
if (newDate != "") { // process non-empty string
    var oMyDate = [];    oMyDate[0] = util.scand("ddmmyyyy", newDate);    oMyDate[1] = util.scand("ddmmyy", newDate);    oMyDate[2] = util.scand("ddmyyyy", newDate);    oMyDate[3] = util.scand("dmmyyyy", newDate);    oMyDate[4] = util.scand("dmyyyy", newDate);    oMyDate[5] = util.scand("dmyy", newDate);
    var isDate = false;

    for (var i=0; i<oMyDate.length; i++) {
        if (oMyDate[i] !== null) {
            event.value = util.printd("dd/mm/yyyy", oMyDate[i]); // strict format            isDate = true;
            break;
        }
    }

    if (isDate === false) {        app.alert("Invalid date entered! Expected dd/mm/yyyy (e.g. ...)", 0, 1, "Date Validation"); // check validity
        event.value = "";
    }
}
This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
November 12, 2023

You need to add a condition that checks the length of the string the user entered.

If it's 8, use "ddmmyyyy". If it's 6, use "ddmmyy".