Adding a date with certain days based on a response from yes no question (using an if-statement)

Hi @Kal_Lam

Can we set a condition on the form’s date field like

if {accepted} is yes , then date selection can not more than 14 days from today ,
if {accepted} is NO, then date selection can not more than 03 days from today

Cheers !

Neeraj

Did not understand the accepted you are referring to. Could you kindly simplify them?

accepted is a select_one field

Hi @Kal_Lam

Please help accepted is a select_one field with Yes/No Option

Cheers !

Neeraj

You could do it as outlined in the image shared below:

In the survey tab of your xlsform:

In the choices tab of your xlsform:

Image 2

Data entry screen as seen in Enketo: when nothing is entered

Data entry screen as seen in Enketo: when yes is entered

Data entry screen as seen in Enketo: when no is entered

Reference xlsform:

Adding days in a date with if-statement.xls (43 KB)

Hi @Kal_Lam

Thanks for response , But what i need is that the date selection should not be allowed ( restricted ) based on condition

if {accepted} is yes , then date selection can not more than 14 days from today ,
if {accepted} is NO, then date selection can not more than 03 days from today

that means if today is 14/05/2021 , selection of date should not be allowed 28-05-2021 or 17-05-2021 respectively

Cheers !

Neeraj

Hi @Kal_Lam

i tried this in calculation field but not working

if(${accepted} = ‘No’,(.>today() and .<today()+3),(.>today() and .<today()+14))

Please help

Hi @Kal_Lam

Please help , i am not able to do so.

Cheers !

Neeraj

Hi @caneeraj
Create a date question and add a constraint using the calculate row provided by @Kal_Lam ${date2}. You need to add the following in constraint

.=${date1} and .<=${date2}

Regards,
Stephane

1 Like

Hello,
Hint: today() will dynamically be re-calculated, for example when you re-open the form on the device or later when you edit a submission on server level. So, the previously entered day may become invalid on new save.

You can avoid this by

  • using the metadata variable today type (. > ${today})
  • or creating a fixed calculate variable with now( today() ).
    This will provide the (constant) date of the first save.

You may need to sharpen the requirement what you exactly want to control when, taking into account later re-edit of the form data.

2 Likes

Hi Every one ,

Thanks i was able to do by using if condition in constraint column.

if(${accepted} = ‘No’,(.>today() and .<today()+3),(.>today() and .<today()+14))

Thanks @wroos , your hint was actually good.

Cheers !

Neeraj

2 Likes

Hello,
So, the problem was just using the wrong column: calculation instead of constraint?
I also adapted the tags of the thread (… constraint).

@caneeraj, maybe you could do it as outlined in the image shared below:

In the survey tab of your xlsform:

In the choices tab of your xlsform:

Image 2

Data entry screen as seen in Enketo: date entered outside the range

Data entry screen as seen in Enketo: date entered within the range

Data entry screen as seen in Enketo: date entered outside the range

Reference xlsform:

Adding days in a date with if-statement.xls (43.5 KB)