Avatar

Correct answer by
Level 7

The simplest approach would be for there to be 3 fields with Y, M, D however if this isn't possible then you would need to first determine how you want it the user to enter the info. For example if you want them to enter 3 years, 7 months 45 Days what is the format you expect? Is it"

03, 07, 45 or 03-07-45 or even 03y, 07m, 45d?

Once you determine that, then you will need to use a regular expression to maintain the format. You will also need a warning if the format is not correct and remove the incorrect entry.

Below is a similar script I created for a formatted date field. You should be able to modify this for your format:

var myRegExp = /[0-1][0-9]\/[0-3][0-9]\/\d{4}/; // Created a regular expression for the date format

var myText = ExecFromDate.rawValue; //variable to store the field

if(ExecFromDate.rawValue == null){ //checking for empty field

ExecFromDate.rawValue = null;

}

else

if(myRegExp.test(myText)== false){ //validate input against regular expression

xfa.host.messageBox("You have entered an incorrect date format. Please use MM/DD/YYYY", "Date error", 3, 0); //warning if incorrect format

xfa.host.setFocus("ExecFromDate") //sets the user back to the date field

ExecFromDate.rawValue = ""  //clears the entry

}

View solution in original post