Set default value for date picker on form 2.0

Anonymous
Not applicable

Set default value for date picker on form 2.0

If I specify a "Today" in the form editor the value does not update. So tomorrow it would still read 2/22/2017. What I want, is for the default value in the field to be set to TODAY such that it updates for the current day. What is the best way to accomplish this? Or is the documentation someone can point me towards.


Thanks!

Tags (2)
10 REPLIES 10
Josh_Hill13
Level 10 - Champion Alumni

Re: Set default value for date picker on form 2.0

Use the {{system.dateTime}} token in a CDV flow step triggered by Fills Out Form will work.

More clearly:

Fills Out Form

Change Data Value: Date = {{system.dateTime}}

You can probably take the Date field out of your form unless you are asking the person for a specific date.

Jason_Reber__TH
Level 2

Re: Set default value for date picker on form 2.0

Hi Dean,

I have the same issue I think - I have a date input on a form, and I'd like to set the default value in that input = the date on which the user views the form. I tried specifying a Default Value of "{{system.date}}" in the field in the Form Builder in Marketo, but it doesn't work when viewing the live form.

@Josh - I believe what you have proposed would change the value only after the user had submitted the form, and would not change the default value seen by the user before they have submitted the form. Anyone have any ideas how to accomplish this from within Marketo?

SanfordWhiteman
Level 10 - Community Moderator

Re: Set default value for date picker on form 2.0

Like so:

MktoForms2.whenReady(function(form){

  var now = new Date();

  form.setValues({

    YourFormField : [now.getFullYear(),now.getMonth()+1,now.getDate()]

                    .join("-")

                    .replace(/-(\d)\b/g,"-0$1")

    });

});

Avoid the temptation to use the simpler-seeming

new Date().toISOString().substr(0,10) 

because that will give timezone-related surprises.

Caroline_Graha1
Level 1

Re: Set default value for date picker on form 2.0

Sanford Whiteman ,

I am having the same issue as Josh; we would like to be able to add this custom code in place in order to restrict users from choosing dates in the past/force them to instead select a future date.

How do I go about adding in the code below to my form?

Thanks in advance for any help!

Caroline

SanfordWhiteman
Level 10 - Community Moderator

Re: Set default value for date picker on form 2.0

You would enclose it in a <script> tag and place it just before the closing </body> (if on a Marketo LP) or just after the standard embed code (if a non-Marketo page).

Caroline_Graha1
Level 1

Re: Set default value for date picker on form 2.0

This may be a silly question, but what would the code look like if I just wanted the form to not accept a date less than 30 days out from the current date?

SanfordWhiteman
Level 10 - Community Moderator

Re: Set default value for date picker on form 2.0

It's not a silly question. A basic datepicker (including the native datepicker when browsers support it, and Marketo's simple polyfill widget) doesn't restrict input like that. I recommend you set the widget type to Text in Form Editor and use a 3rd-party datepicker on it instead.

I could provide code to validate that a date is in a certain range when they hit Submit, but that's probably not what you mean.

Chase_Carson
Level 1

Re: Set default value for date picker on form 2.0

Hi Sandford,

 

If I just wanted to replace the default value on the date field mm/dd/yyyy to a french format (m/jj/aaaa). Do you know how I would do that? Or is there JS to modify the calendar widget itself as right now it is in English

SanfordWhiteman
Level 10 - Community Moderator

Re: Set default value for date picker on form 2.0

Jason Reber (THV)​ bumping this in case you didn't see my answer.