Expand my Community achievements bar.

SOLVED

Limit date field to NOT allow random text?

Avatar

Former Community Member

Hi there,

I am using LiveCycle ES3.  This may seem like a silly question, but how do I prevent users from entering random, non-date text, like "adsfasdfkafsdj" into a date field?  I have set the field to required, and set the edit/validation/display validation patterns as indicated below, but still I can enter random text into the field. It pops up my custom error message saying the date is in the wrong format and to re-enter it correctly, but then the cursor just skips to the next field, leaves the incorrect text in the date field, and doesn't require the user to correct it. 

Display Pattern

date{MM/DD/YY}

Edit Pattern

date{M/D/YY}|date{M/D/YYYY}|date{MM/DD/YY}|date{MM/DD/YYYY}|date{M/DD/YY}|date{M/DD/YYYY}|date{MM/D/YY}|date{MM/D/YYYY}|date{M.D.YY}|date{M.D.YYYY}|date{MM.DD.YY}|date{MM.DD.YYYY}|date{M.DD.YY}|date{M.DD.YYYY}|date{MM.D.YY}|date{MM.D.YYYY}|date{M-D-YY}|date{M-D-YYYY}|date{MM-DD-YY}|date{MM-DD-YYYY}|date{M-DD-YY}|date{M-DD-YYYY}|date{MM-D-YY}|date{MM-D-YYYY}|date{MMDDYY}|date{MMDDYYYY}

Validation Pattern

date{M/D/YY}|date{M/D/YYYY}|date{MM/DD/YY}|date{MM/DD/YYYY}|date{M/DD/YY}|date{M/DD/YYYY}|date{MM/D/YY}|date{MM/D/YYYY}|date{M.D.YY}|date{M.D.YYYY}|date{MM.DD.YY}|date{MM.DD.YYYY}|date{M.DD.YY}|date{M.DD.YYYY}|date{MM.D.YY}|date{MM.D.YYYY}|date{M-D-YY}|date{M-D-YYYY}|date{MM-DD-YY}|date{MM-DD-YYYY}|date{M-DD-YY}|date{M-DD-YYYY}|date{MM-D-YY}|date{MM-D-YYYY}|date{MMDDYY}|date{MMDDYYYY}

With the patterns I have above, the user should ONLY be allowed to enter data in one of these formats, correct?  I'm at a loss as to why this isn't working the way it's supposed to.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

this is also possible with a script in the exit event of the date field.

It uses a regular expression to check the entered value for a specific pattern.

Allows are dates in these way: [9|99] [.|/|-] [9|99] [.|/|-] [99|9999].

if (! this.formattedValue.match(/^\d{1,2}[\.\/\-]\d{1,2}[\.\/\-](\d{4}|\d{2})$/g)) {

          xfa.host.messageBox("Enter a correct date!", "Invalid Date", 0, 0);

          xfa.host.setFocus(this.somExpression);

}

View solution in original post

4 Replies

Avatar

Level 10

Hi,

you can prevent this with a small JavaScript in the change event of a date field.

It checks if the current input is not a number, a dot, a slash or a hyphen.

If so, the entry is replaced by "" (nothing.

if (!xfa.event.change.match(/[\d\.\/-]/g)) {

          xfa.event.change = "";

}

Avatar

Former Community Member

Thank you!  How do I make it so they have to enter an actual date?  With the script you gave me, they can't enter random text anymore, which is good, but they can still enter random numbers, dots, slashes, and hyphens in any order and while the field will generate an error message, it will not reject the incorrect entry.  I want the user to only be able to enter a valid date in one of the edit patterns I listed above, and if they don't then the field should either not allow it and/or it should delete their incorrectly-formatted entry. 

Avatar

Correct answer by
Level 10

Hi,

this is also possible with a script in the exit event of the date field.

It uses a regular expression to check the entered value for a specific pattern.

Allows are dates in these way: [9|99] [.|/|-] [9|99] [.|/|-] [99|9999].

if (! this.formattedValue.match(/^\d{1,2}[\.\/\-]\d{1,2}[\.\/\-](\d{4}|\d{2})$/g)) {

          xfa.host.messageBox("Enter a correct date!", "Invalid Date", 0, 0);

          xfa.host.setFocus(this.somExpression);

}