Customize your Marketo forms even more by using these 3 advanced Marketo form techniques. Learn how to show a thank you message after a form submission, how to limit word count on text fields and how to block free email addresses from form submissions.
This is part 2 of the advanced Marketo form techniques series. View part 1.
Show a thank you message after form submission
This snippet will show a thank you message after a successful form submission, instead of redirecting to a thank you page.
Add this piece of javascript code to your page:
1 2 3 4 5 6 7 |
MktoForms2.whenReady(function (form) { form.onSuccess(function(values, followUpUrl) { form.getFormElem().hide(); document.getElementById("successAndErrorMessages").innerHTML="YOUR THANK YOU MESSAGE HERE"; return false; }); }); |
Add this HTML code where you want the thank you message to display on the page:
1 |
<div id="successAndErrorMessages"></div> |
Limit word count for a textarea & display the # of words left
Limit the number of words a user can input in a text field or textarea with this script.
Add this HTML to the label in your form.
1 |
Total word count: <span id="display_count">0</span> words. Words left: <span id="words_left">250</span> |
Add this piece of javascript to your landing page. Change #comments to the name of your field and update 250 to the number of words you want to limit the field to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$(document).ready(function() { MktoForms2.whenReady(function (form) { // Update the ID with the ID of the form field $("#comments").keyup(function() { var words = this.value.match(/\S+/g).length; if (words > 250) { /* update 250 to the # of words you want to limit */ var trimmed = $(this).val().split(/\s+/, 250).join(" "); /* update 250 to the # of words you want to limit */ $(this).val(trimmed + " "); } else { $('#display_count).text(words); $('#words_left').text(250-words); /* update 250 to the # of words you want to limit */ } }); }); }); |
Block free email addresses from filling out a form
Block @gmail, @yahoo.com, @hotmail.com, @live.com, @aol.com and @outlook.com email addresses from filing out a form and require a business email address by adding this script to your landing page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
(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){ form.onValidate(function(){ var email = form.vals().Email; if(email){ if(!isEmailGood(email)) { form.submitable(false); var emailElem = form.getFormElem().find("#Email"); form.showErrorMessage("Must be Business email.", emailElem); }else{ form.submitable(true); } } }); }); function isEmailGood(email) { for(var i=0; i < invalidDomains.length; i++) { var domain = invalidDomains[i]; if (email.indexOf(domain) != -1) { return false; } } return true; } })(); |
Questions?
Email me, send me a tweet @jennamolby, or leave a comment
Hi Jenna, thanks for this. For “Add this piece of javascript code to your page:” – where exactly do I add? The landing page? or the Form’s custom CSS? If it’s the landing page, do I insert the or Rich Text element to the page? If you can add screenshot of how and where you add to this page, that would be great for people with no coding background 🙂
Hi Hanna, I recommend placing it within your landing page template right before the closing </body> tag. Cheers, Jenna
Hi Jenna, Do you have any examples on how to include multiple forms on one page
Hi Jenna, thank you for this (and other) excellent Marketo posts.
Question: in the ‘Show a thank you message after form submission’ snippet, does the form ID have to be in that code?
Hi Peter, No the form ID doesn’t have to be in that code if you have it on a Marketo landing page. If you have it on a non-Marketo landing page you should include the form ID or if you have more than one form on a page. Cheers, Jenna
I know this article is old (still highly useful though), but I am having trouble using the thank you message code in a Marketo template. I think it’s because the template I’m creating allows a form to be picked and changed in the guided landing page editor, instead of using the form’s embed code in the template. I get errors saying that MktoForms2 isn’t defined. Any tips?
Hi Evan, It still should work if you’re using the guided landing page editor. Send the link to your landing page to [email protected] and I can help you troubleshoot.