Allow user to enter only numbers for a date and populate with slashes, then validate

Hello,
I'm using Acrobat X. I have many forms with many date fields that I would like to allow the user to fill in with just numbers and populate it with slash separators as soon as they press TAB. Right now I'm using Format > Special > Arbitrary Mask, with a value of "99/99/9999". This works in terms of formatting, but doesn't allow for date validation. I don't like to use the built-in date formatting because it requires the user to type the slashes (or dashes).

The other problem with using the "99/99/9999" method is that if you want to go back and correct your date, if you click in the middle of the field and press either BACKSPACE or DELETE, it pops up an error, "The value entered does not match the format of the field...". The only way you can correct it without an error is if you position the cursor at the very end of the field and press BACKSPACE.

So what I would really like to do is allow the user to move quickly by just typing numbers (no slashes or dashes), have the slashes populate automatically upon pressing TAB, and perform at least some rudimentary date validation. I tried using some of the code below which I borrowed from other people, but I still couldn't make it work.

I'd greatly appreciate any help.

// adding slashes
if (event.value!="") event.value = event.value.substring(0,2) + "/" + event.value.substring(2,4) + "/" + event.value.substring(4)

// validate date

if(event.value != "") { // process non-empty string
var oMyDate = util.scand("dd-MM-yyyy", event.value); // convert to date object
if(oMyDate == null) app.alert("Invalid date entered!", 0, 1), "Date Validation"; // check validity
event.value = util.printd("dd-MM-yyyy", oMyDate); // strict format
}


Kamran Grasselli


2 Answers

Voted Best Answer

Try this as the Custom Validation Script of a text field:

// 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 = "";
    }
}


By Ricardo Falegnami   

The sequence of the month day and year are needed to properly validate the date. I have worked with personnel systems that enter the date in "yyyymmdd" year format for ease in performing manual date difference calculations. Some countries use "mm/dd/yyyy" while others use "dd/mm/yyyy" as the default standard.


George Kaiser   


Please specify a reason: