How to make field mandatory based on condition and concating dates

Avatar

Avatar

ktpp

Avatar

ktpp

ktpp

15-09-2015


Hi I am currently trying to accomplish a few things using adobe live cycle and java script.  I am in need of assitance and would be very greatful for any help.  I have provided the scenario below.

A user enters a date into a pdf fillable form by entering information into 6 text fields.  One text field for the 4 digits of the year e.g. 2015 and 2 for the month e.g. 09 (September).  I would like to make another field textfield Y be mandatory if the date (the one with the 6 text boxes) -todays date <1865 days.  Please note I am not able to change the form design unfortunately.    In order to help I have provided a little more detail below.

Lets say a user has a date of Sep 15, 2015 then they enter 2 in one text field, 0 in another, 1 in another, 5 in another, 0 in another and 9 in the last field.  The rule is if today's date-that date they entered is less than 5 years they must provide additional information.

I have concated the the 6 text fields to construct a date that is "YYYY-MM" and have tried to put in the rule but  the dates are not being read properly.  Any help would be much appreciated.  Some of my code is below.

To re-recreat the date I created a text field and using form calc on calculate event. M2R =the second month digit

 

if (M2R.rawValue ne null)

then

 

$.formattedValue

= (CONCAT(Y1R,Y2R,Y3R,Y4R,"-",M1R,M2R))

To capture the date for today I simply use this code in another text box

 

$.rawValue

= Num2Date(Date(), "MMM-YY")

To get the difference I take I simply create a new variable =Concatted date-today's date in milliseconds, then convert it into days into a variable called days.  I try to capture this in a numerfic field. 

To set mandatory field I say fieldname.mandatory=(days.rawvalue<1865)?"error:"disabled"

Accepted Solutions (0)

Answers (4)

Answers (4)

Avatar

Avatar

ktpp

Avatar

ktpp

ktpp

16-09-2015

Hey great thanks for this.  I tried and the only  thing that seems to work is to use form calc however it say"-" object can not be accessed. 

Avatar

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150
Magus069
MVP

15-09-2015

Alright a few things might be in consideration in this case,

1. I see you have DATEFIELD2 and DateField1, but when creating regularDate1 you are missing the capital F var regularDate1 = new Date(DateField1.rawValue.replace(/-/g, "/"));

2. The new Date() constructor have 4 different constructors :

     i. Date() as today's date

     ii. Date(milliseconds)

     iii. Date(DateString) E.g.:"October 13, 2014 11:13:00"

     iv. Date(year, month, days, hours, minutes, seconds, milliseconds)

3. If you are to use any of these constructors instead of FormCalc,

    it would be a great idea to have DateField1's rawValue to be using this format "YYYY-MM-DD" or "YYYY-MM" as Data,

    you can change the formattedValue within the options to your likings

So on the event of your choice you can use the following code to retrieve the difference between 2 dates

Hope this will help ya!

Avatar

Avatar

ktpp

Avatar

ktpp

ktpp

15-09-2015

I have created a demo form however can not find how to upload it.  All the code is below.  The 6 fields which date 2 gets data from are (Y1R,Y2R etc).  I think I am good with the mandatory fields. The problem seems to be reading the dates and taking the difference.

form1.#subform[0].DATEFIELD2::calculate - (FormCalc, client)

if (M2R.rawValue ne null) then

  $.rawValue = (CONCAT(Y1R,Y2R,Y3R,Y4R,"-",M1R,M2R))

  else

  $.rawValue = ""

  endif

form1.#subform[0].DateField1::ready:layout - (FormCalc, client)

$.rawValue = Num2Date(Date(), "MMM-YY")

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

//transform fields to dates

   var regularDate1 = new Date(Datefield1.rawValue.replace(/-/g, "/"));

   var regularDate2 = new Date(DATEFIELD2.rawValue.replace(/-/g, "/"));

  

   // Get difference between dates in milliseconds

   var milliseconds = regularDate1.getTime() - regularDate2.getTime();

  

   // Define number of milliseconds in one day

   var nMilliSecondsPerDay = 24 * 60 * 60 * 1000;

   // Get difference in days

   var days = Math.floor(milliseconds / nMilliSecondsPerDay);

   //Display value in Numeric1 field

   textfield4.rawValue = days;

Avatar

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150

Avatar

Magus069

MVP

Total Posts

1.0K

Likes

132

Correct Answer

150
Magus069
MVP

15-09-2015

Hi there,

everything seems okay according to what you provided, maybe there would be a syntax error like this : fieldname.mandatory=(days.rawvalue<1865)?"error:"disabled"

which would be fieldname.mandatory= days.rawValue < 1865 ? "error" : "disabled";

otherwise it depends what is not working, could you provide the form, so I can have a look