Expand my Community achievements bar.

SOLVED

Custom date validation script

Avatar

Level 5

I have the script below that I used for a PDF form in Acrobat X, but it doesn't work when I enter it in the Javascript editor in ES2.  Is it possible to create a script that does the same thing in LiveCycle Designer ES2?  I would like to let the user enter a date using numbers only, and have the field add in slashes as needed, in addition to validating for a proper date.   Thanks for any assistance.

//Acrobat date validation

//Custom Validation Java Script for entering dates using numbers only

//courtesy of Ricardo Falegnami

// adding slashes

var newDate = event.value;

// validate date

if (newDate != "") { // process non-empty string

    var oMyDate = [];

    oMyDate[0] = util.scand("mmddyyyy", newDate);

    oMyDate[1] = util.scand("mmddyy", newDate);

    oMyDate[2] = util.scand("mddyyyy", newDate);

    oMyDate[3] = util.scand("mmdyyyy", newDate);

    oMyDate[4] = util.scand("mdyyyy", newDate);

    oMyDate[5] = util.scand("mdyy", newDate);

    var isDate = false;

    for (var i=0; i<oMyDate.length; i++) {

        if (oMyDate[i] !== null) {

            event.value = util.printd("mm/dd/yyyy", oMyDate[i]); // strict format

            isDate = true;

            break;

        }

    }

// Stay in field if invalid

    if (isDate === false) {

        app.alert("Invalid date entered -- please use MMDDYYYY (e.g., 10152014)", 0, 1, "Date Validation"); // check validity

        event.value = "";

        event.target.setFocus();

    }

}

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

I'm guessing you are putting this in the exit event of the field.  If so, change the three occurrences of event.value to this.rawValue and the event.target.setFocus() to xfa.host.setFocus(this)

Regards

Bruce

View solution in original post

10 Replies

Avatar

Correct answer by
Level 10

Hi,

I'm guessing you are putting this in the exit event of the field.  If so, change the three occurrences of event.value to this.rawValue and the event.target.setFocus() to xfa.host.setFocus(this)

Regards

Bruce

Avatar

Level 5

That worked great!  One more question:  The field is now displaying the current date by default.  Is there a way to just make it be blank for entry by user?  Thanks again.

Here's the edited script for the benefit of others:

//Custom Date Validation Java Script for entering dates using numbers only

//courtesy of Ricardo Falegnami and BR001

// adding slashes

var newDate = this.rawValue;

// validate date

if (newDate != "") { // process non-empty string

    var oMyDate = [];

     oMyDate[0] = util.scand("mmddyyyy", newDate);

    oMyDate[1] = util.scand("mmddyy", newDate);

    oMyDate[2] = util.scand("mddyyyy", newDate);

    oMyDate[3] = util.scand("mmdyyyy", newDate);

    oMyDate[4] = util.scand("mdyyyy", newDate);

    oMyDate[5] = util.scand("mdyy", newDate);

    var isDate = false;

 

    for (var i=0; i<oMyDate.length; i++) {

        if (oMyDate[i] !== null) {

            this.rawValue = util.printd("mm/dd/yyyy", oMyDate[i]); // strict format

            isDate = true;

            break;

        }

    }

// Stay in field if invalid

    if (isDate === false) {

        app.alert("Invalid date entered -- please use MMDDYYYY (e.g., 10152014)", 0, 1, "Date Validation"); // check validity

        this.rawValue = "";

        xfa.host.setFocus(this)();

    }

}

Avatar

Level 5

[Hello, please see above.]

The field is now displaying the current date by default.  Is there a way to just make it be blank for entry by user?

Avatar

Level 10

Hi,

Try changing the line;

if (newDate != "")

to

if (newDate != null)

An empty date field is a null, not an empty string.

Regards

Bruce

Avatar

Level 5

Thanks again for the help.  Reposting corrected script for future users:

//Custom Date Validation Java Script for entering dates using numbers only

//courtesy of Ricardo Falegnami and BR001

// adding slashes

var newDate = this.rawValue;

// validate date

if (newDate != null) { // process non-empty string

    var oMyDate = [];

     oMyDate[0] = util.scand("mmddyyyy", newDate);

    oMyDate[1] = util.scand("mmddyy", newDate);

    oMyDate[2] = util.scand("mddyyyy", newDate);

    oMyDate[3] = util.scand("mmdyyyy", newDate);

    oMyDate[4] = util.scand("mdyyyy", newDate);

    oMyDate[5] = util.scand("mdyy", newDate);

    var isDate = false;

    for (var i=0; i<oMyDate.length; i++) {

        if (oMyDate[i] !== null) {

            this.rawValue = util.printd("mm/dd/yyyy", oMyDate[i]); // strict format

            isDate = true;

            break;

        }

    }

 

// Stay in field if invalid

    if (isDate === false) {

        app.alert("Invalid date entered -- please use MMDDYYYY (e.g., 10152014)", 0, 1, "Date Validation"); // check validity

        this.rawValue = "";

        xfa.host.setFocus(this)();

    }

}

Avatar

Level 5

After looking at this script carefully, I was wondering what the triple equals sign does in the line:

    if (isDate === false)


I have only seen double equal signs, not triple.  Thanks.

Avatar

Level 10

Hi,

The triple equals avoids any type conversions, in this case isDate and the false literal are both boolean so it doesn't make much difference, false == false and false === false both evaluate to true, but "" == false evaluates to true but "" === false evaluates to false.  It's just a good habit to get into.

Here's a link to a guy that can explain it better, ECMA-262 » Note 2. ECMAScript. Equality operators.

Regards

Bruce

Avatar

Level 5

Hi again,

If I want to use the above script and just change the date format to yyyymmdd, can I simply change the order of the letter in the format masks to achieve the same result?

Thanks.

Avatar

Level 5

Hello Bruce,

I've noticed that you can enter "2015013" (7 digits) and it produces "2015/01/13" or even enter "201566" (6 digits) and it produces "2015/06/06".  Can you change the script so that it requires 8 digits.  I tried doing it myself and ended up crashing Acrobat when I would try to open the file. Oops.  :/

Avatar

Level 10

Hi,

You should be just be able to remove the util.scand lines that aren't 8 digits (so all but the first one).

Regards

Bruce