Cognito Forms – How to Prefill Date Field When Embedding

cognito-forms

I am trying to prefill a date field for an embedded Cognito form.

Below is shown the JavaScript code which I'm using in order to preload a form for the Controls Textbox, MultiLine Textbox, Radio Buttons, Date Field and Drop Downs. But the Date of Birth, which is a Date Field, does not populate with the prefill data.

Cognito.load("forms", { id: "1" , entry: {
                    "FormId": getParameterByName("encid"),
                    "Name": getParameterByName("name"),
                    "Address": getParameterByName("addr"),
                    "Date of Birth": getParameterByName("dateofbirth"),
                    "Sex": getParameterByName("sex"),
                    "Form Number": getParameterByName("formnumber"),
                } });

What is the syntax/format for prefilling dates in Cognito Forms?

Best Answer

Great question!

Dates are tricky things on the web due to a lack of an official standard for formatting. However, Cognito Forms uses the defacto standard, ISO 8601, as the format for dates in the JSON format. So in your example, the date would need to be something like "1962-03-29" in order to be processed. Also, when prefilling, the value can be a real JavaScript date, which is not supported in JSON, but is supported by prefill because it accepts a real JavaScript object.

However, this is likely not the cause of your issue. In order to ensure form field names are unique and well-formed in the JSON and code, the field label is used to create a field name that is used when writing calculations, converting to JSON, exporting to Excel, etc. So the name of your field in the JSON likely should be DateOfBirth instead of "Date of Birth".

In order to see the full structure of the form you are trying to prefill, we recommend using RequestBin to see what your form entries look like as JSON. Click on Create a RequestBin, copy the url, select Post JSON data to a website? under Submission Settings, paste the RequestBin url and save your form. Then fill out your form just like your end users would and refresh the RequestBin page to see the JSON.