How to display age as years, months and days?

leighh77457178

10-10-2018

Hi

I have a form created in livecycle designer and one of the fields is age at diagnosis - I want to format the field to allow the user to enter the age the person was at diagnosis manually (its not calculated from date of birth (DOB) or from any other date its just an age that the  person completing the form would know). If I want to manually add this to a field how can I format it so that the user enters the age in years months and days? Currently we are experiencing a few problems as some people are using the field to enter the age in months and then some are entering the age in years so its a bit confusing when trying to analyse this data.

I want to make it as easy as possible to enter the age in the format I want i.e. years months and days.

thanks in advance!

Accepted Solutions (1)

Accepted Solutions (1)

mouslander

10-10-2018

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

}

Answers (9)

Answers (9)

mouslander

10-10-2018

No. The field will need to be "Text" to allow the "Y, M and D" chars. If you provide your email address I can send my sample for  you to try.

mouslander

10-10-2018

I played with the regular expression. It is not perfect but should provide help:

var myRegExp = /[0-1][0-9]+Y [0-1][0-9]+M [0-3][0-9]+D/;      // Created a regular expression. This will only allow 2 digits for the year with                                                                                                 // the "Y" for year, 2 for month with an "M" 2 for days with a "D". This does not                                                                                                 // restrict an incorrect entry for example if the user puts in 14 months it will                                                                                                 //allow it.

var myText = ExecFromDate.rawValue;

if(ExecFromDate.rawValue == null){

ExecFromDate.rawValue = null;

}

else

if(myRegExp.test(myText)== false){

xfa.host.messageBox("You have entered an incorrect Years Months and Days format. Please see sample: 12Y 11M 09D", "Date error", 3, 0);

xfa.host.setFocus("ExecFromDate")

ExecFromDate.rawValue = "false"

}

mouslander

10-10-2018

Yes you would have to look at the RegExp to determine and set the format. If you look at my current format in my sample it ensures that the the entry is mm/dd/yyyy no other format or variation will be allowed.You would need to find the correct RegExp format

leighh77457178

11-10-2018

this is the current script on the text field in my form where would I add your script? in the initialize line?

form1.#subform[0].ageatfirstseizure::initialize - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::enter - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::exit - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::calculate - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::validate - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::validationState - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::mouseEnter - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::mouseExit - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::change - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::full - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::mouseUp - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::mouseDown - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::click - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::preSave - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::postSave - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::prePrint - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::postPrint - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::preSubmit:form - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::postSubmit:form - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::docReady - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::docClose - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::ready:form - (JavaScript, client)

 

 

form1.#subform[0].ageatfirstseizure::ready:layout - (JavaScript, client)

 

leighh77457178

10-10-2018

Dear Mouslander

Thank you so much for your timely response. I would like the format to be 03y 07months 45days.

Would the script you have very kindly shared work to format the field like that? With some minor modifications? I am new to scripting but I could give it a try!!!

best wishes

leigh