pasted image 0.png

Common Form Questions and Answers

Level 8

Over the years here at the Marketing Nation, I've seen many basic form questions come over and over again with common functionalities. Just so there's a central repository of how to handle these requests, I've compiled a quick post that goes over form functions.

Do note that the vast majority of these response require you to know a little bit of HTML and JavaScript (or work with someone who can deploy JavaScript on your page) and are globally scoped—include the script and they'll fire on any form that's on the same page as the form. If you plan to use multiple forms on one page, you may need to alter these snippets to target specific forms.

Frequently Asked Questions

My team is asking me about form values being prefilled with the forms we have on our site. How do I do this?

Marketo's form code does not support prefill in places outside of Marketo landing pages due to security concerns. The only non-API way to use things like prefill is to create an iframe (and ideally something like pym.js to handle the iframe itself.)

How do I record Google Analytics events upon form submit?

My preferred method for doing this is simply to do a slight modification of your embed code to include a formName variable, so you'd go from something like

<script>MktoForms2.loadForm("//", "123-ABC-456", 1);</script>


<script>MktoForms2.loadForm("//", "123-ABC-456", 1); var formName = "Contact Us form";</script>

From there, it's simply a manner of adding the following to your site's overall JavaScript:

<script type="text/javascript">

MktoForms2.whenReady(function(form) {

form.onSuccess(function(vals, page) {

ga('send', 'event', {

eventCategory: 'Marketo Form',

eventAction: 'Form Submission',

eventLabel: formName,

hitCallback: function() {

document.location.href = page;



return false;




Do note that if you're using User ID tracking that you may need to explicitly set it in the onSuccess function before sending the event.

How do I show a thank you message on the same page upon a form being submitted rather than redirecting to a new page or just showing the form again?

You'll need to make a div containing the messaging you want to display and place it in the same parent element as your form, similar to the following:

<div class="container">

<script src="//"></script>

<form id="mktoForm_27"></form>

<script>MktoForms2.loadForm("//", "123-ABC-456", 1);</script>

<div id="confirmation" aria-hidden="true" style="display:none;">

<p>This is a sample thank you message. Thanks for viewing.</p>



Then, simply include the following in your site's JavaScript:

<script type="text/javascript">

MktoForms2.whenReady(function (form){

form.onSuccess(function(vals, page){


var confirm = document.getElementById('confirmation'); = 'inline-block';

confirm.setAttribute('aria-hidden', false);


return false;




You can also get creative with this and use a mktoText area in your Marketo landing page template to allow dynamic thank you messages to be added without needing a second page!

How do I block certain domains from filling out my forms?

First off: please, don't do this. There are few reasons to do this, especially because most people want to block free domains or certain characters. There are quite a few people, especially in technical industries, who will use disposable addresses or character separators for filtering early in your funnel. Irritating people who want to engage with you, regardless of their email, is not going to help much. However, if you must:

<script type="text/javascript">

(function (){

// Please include the email domains you would like to block in this list

var invalidDomains = ["@gmail.","@yahoo.","@hotmail.","@live.","@aol.","@outlook."];

MktoForms2.whenReady(function (form){


var email = form.vals().Email;


if(!isEmailGood(email)) {


var emailElem = form.getFormElem().find("#Email");

form.showErrorMessage("Must be Business email.", emailElem);







function isEmailGood(email) {

for(var i=0; i < invalidDomains.length; i++) {

var domain = invalidDomains[i];

if (email.indexOf(domain) != -1) {

return false;



return true;




How can I improve email data quality entered from forms?

I like a little script called Mailcheck, which provides suggestions when people misspell email addresses. Include jQuery and mailcheck.js on your page (or reference it via cdnjs) and then use the following:

<script type="text/javascript">

MktoForms2.whenReady(function (form){

var domains = ["", "", "", "", "", "", "","", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", "", "", "","", "" , "", "", "" ,"", "", "", "" , "", "","","","", "", "", "", "","", "", "", "", "","", "", "", "", "", "","", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""];

var topLevelDomains = ["", ".ca", "com", "net", "org", "info", "edu", "gov", "mil"];

var selector = '#Email, .mktoEmailField, .mktFormEmail, input[type=email]';

$(selector).on('blur', function(){


domains: domains,

topLevelDomains: topLevelDomains,

suggested: function(element, suggestion) {

var $parent = $(selector).parent();

$('.mailcheck-msg', $parent).remove();

$parent.append('<span class="mailcheck-msg">Did you mean <a href="#" class="mailcheck-suggestion">' + suggestion.full + '</a>?</span>');



$('a.mailcheck-suggestion').on('click', function(){

$(selector).val( $(this).html() );


return false;






How do I include links (or other HTML) on a Marketo field label?

This is commonly used to link to terms and conditions, privacy policies and the like on checkboxes. Simply grab the API name of the field you're trying to manipulate and use the following structure:

<script type="text/javascript">



var termsBox = document.querySelector("[for='acceptTerms']");

termsBox.innerHTML = "Check here to indicate that you have read and agree to the terms of the <a href='privacy-policy-link'>Privacy Policy</a>.";




How can I prevent spam entries to my form?

One of the biggest ongoing problems Marketo users who have forms run into is ensuring their forms are free of automated spam. As spam bots have become more sophisticated, traditional honeypot and captcha methods haven’t worked, but at the same time, end users don’t want to have to prove they’re not a robot!

Google introduced the Invisible reCAPTCHA in late 2016 to handle this problem more elegantly, and building the technology into your forms is a straightforward affair. To get started, visit and fill out the “Register a New Site” section:

pasted image 0.png

Select “Invisible reCAPTCHA” and add any domains you plan on having forms on in the Domains box. Do note that you only need your base domain, so and will be covered by Accept the terms and register. Once you register, you’ll see an area with two keys:

pasted image 0 (1).png

Keep both of these keys handy, because we’ll be using them!

Meanwhile, head over to Marketo and create two fields in Admin->Field Management>: a string called “spamCheck” and a boolean field called “Verified”. These will be used to check the spam challenge and route your records accordingly.

If you don’t have jQuery installed on your page already, make sure you have a copy of jQuery, and then implement the following script after jQuery loads:

<script type="text/javascript">

MktoForms2.whenReady(function (form) {

$( "button[type='submit']" ).addClass( "g-recaptcha" ).attr( "data-sitekey", "YOUR-SITE-KEY" ).attr("data-callback","letsGo");

$.getScript( "",);

letsGo = function() {

MktoForms2.whenReady(function (form) {

var v = grecaptcha.getResponse();

form.vals({"spamCheck" : v});






This will load Google’s reCAPTCHA API on your page. In short, what will happen is that when a user submits your form, the form will quickly call Google’s reCAPTCHA service to check that the submitter isn’t a spam bot. Google then returns a response, which we’ll store in the spamCheck field in Marketo. However, this is only half of the equation: we need to use our private key to check that the response is valid. Thankfully, we can do this with a simple webhook.

Head to Admin->Webhooks> and create a new webhook with the following values:

Webhook Name: ReCAPTCHA Validation


Request Type: GET

Response Type: JSON

Hit “Save”. From there, hit the “Edit” button next to Response Mappings. For your response attribute, type in success (all lowercase) and for Marketo Field, choose the Verified boolean field you created earlier. The final product should look like the following:

pasted image 0 (2).png

From there, using the reCAPTCHA validation is simple; when you set up a trigger for a form to be processed, you’ll simply call the webhook, wait 1 minute, and then do what you wish with the form fillout:

pasted image 0 (3).png

And that’s all there is to it! Do note that per Google's reCAPTCHA policy, you'll be required to show the reCAPTCHA logo and terms of service. This is added by the script itself by default, but you can change how it appears as needed.


Courtney Grimes​ love the point about Mailcheck. We were just talking about email verification tools like and Informatica at the June Manufacturing Virtual User Group​ meeting, but I like this way of empowering the visitor to help with our data hygiene efforts to ensure that we're able to reach them for follow up on engagement opportunities.

Hi Courtney Grimes​, Thank you for addressing the point about mailcheck. However is there any way to restrict the registrants by entering incorrect email address. For example: we have been receiving so many email address like: sant@gmail

They are not providing complete email address and marketo forms does not provide validation check.

Thank you,

Level 10 - Community Moderator

They are not providing complete email address

In fact, user@gmail — or any email address like user@example where example is a syntactically valid DNS label — is a perfectly valid, complete email address, which is why it will pass validation on any HTML form with an <input type="email">.

It isn't, except in rare cases, a publicly routable email address, but that isn't the same thing. This can be confusing to people who are unfamiliar with SMTP.

It could be argued that HTML forms should allow publicly routable email addresses only, but you cannot determine this with string parsing alone: user@example.zz is less publicly routable than user@ai: zz is not currently a valid TLD, while the simple label ai does have an MX record, allowing it to receive mail.

Likewise, may appear to have a private hostname (co being private and uk being public)  but in fact it does not, as together comprise a public suffix, so in practice it's just like user@com.

Without misusing the words "incorrect" or "valid," here is how you can restrict an email field to hostnames with at least 2 parts:

MktoForms2.whenReady(function(form) {   
  form.onValidate(function(nativeValid) {     
    if (!nativeValid) return;     
    var currentValues = form.getValues(),        
    formEl = form.getFormElem()[0],        
    emailEl = formEl.querySelector("[name=Email]"),        
    RE_EMAIL_ASCII_PUBLIC = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;     

    if (!RE_EMAIL_ASCII_PUBLIC.test(currentValues.Email)) {        
        "Must be valid email. <span class='mktoErrorDetail'></span>", MktoForms2.$(emailEl) );     
    } else {        

Again, this is not a "validation" in any sense of the word other than checking for two parts to the right of the @.

If you have further questions, please open a thread in Products​ as this comment section doesn't have proper syntax highlighting and is very annoying to use for code.

Thank you very much Sanford Whiteman​. It works for me.

Not applicable

Thank Courtney for the great material here.  I was able to add the Invisible reCaptcha but not without a little hitch.  I was finally able to make it work by adding the script after the form embed.  My thinking is that when I added it to our WordPress template it was probably getting loaded before the jquery script.  Thanks again.

I haven't been able to login to the Community for months, and look what I missed! This is great Courtney Grimes I have it bookmarked.

Has anyone come across the timeout-or-duplicate error while trying to implement the invisible recaptcha?

Level 10 - Community Moderator

That's not necessarily an "error" in the world of reCAPTCHA -- it's one of the known exceptions returned by the Google endpoint and can result from attacks as well as a bad implementation.

You'll have to supply a lot more information about your setup, such as Call Webhook details, webhook setup, form examples, traces on the wire. And preferably in a new thread in Products​.

Will do! Sorry for my ignorance but what do you mean by traces on the wire?

Level 10 - Community Moderator

In this case I mean the Network tab in Developer Tools (not technically "on the wire" but seeing what you're posting to the server).