Last active
October 3, 2018 16:00
-
-
Save RobertCam/67922f8cdcb0dae4f50daf3051714a24 to your computer and use it in GitHub Desktop.
Use cookies to check if an Unbounce form has already been submitted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
/* | |
Unbounce Community :: Tips & Scripts :: Check if Form Already Submitted | |
TS:0002-04-086 | |
*********************** | |
Do not remove this section. It helps our team troubleshoot possible issues and track feature usage and adoption. | |
*/ | |
// Set amount of time until cookie expires (in Minutes) and form can be submitted again | |
var minutes = 1; | |
// Add ID of field to check for submission | |
var fieldId = "oop_you_already_submitted"; | |
// Add ID of submit button | |
var submitButton = $("#lp-pom-button-10"); | |
// Display messgae when someone tries to double submit | |
var errorMessage = 'You Already Submitted This Form. Clear Browser Cookies to re-submit'; | |
// Do Not Edit Code Below - SET COOKIE | |
function yourSubmitFunction(e, $) { | |
e.preventDefault(); | |
// set cookie | |
function setCookie(co, cvalue, mins) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (mins * 60 * 1000)); | |
// set expirey | |
expires = "; expires="+d.toGMTString(); | |
document.cookie = co+"="+cvalue+expires+"; path=/"; | |
} | |
setCookie("convert", true, minutes); | |
// If your code is asynchronous, call this final line in your callback instead | |
$('.lp-pom-form form').submit(); | |
} | |
lp.jQuery(function($) { | |
$('.lp-pom-form .lp-pom-button').unbind('click tap touchstart').bind('click.formSubmit tap.formSubmit touchstart.formSubmit', function(e) { | |
if ( $('.lp-pom-form form').valid() ) yourSubmitFunction(e, $); | |
}); | |
$('form').unbind('keypress').bind('keypress.formSubmit', function(e) { | |
if(e.which === 13 && e.target.nodeName.toLowerCase() !== 'textarea' && $('.lp-pom-form form').valid() ) | |
yourSubmitFunction(e, $); | |
}); | |
}); | |
// Do Not Edit Code Below - CHECK FOR COOKIE | |
var field = $("#"+fieldId); | |
var label = $(submitButton).children(); | |
var container = $("#container_"+fieldId); | |
$(container).css("display", "none"); | |
// get cookie | |
function getCookie(co) { | |
var name = co + "="; | |
var decodedCookie = decodeURIComponent(document.cookie); | |
var ca = decodedCookie.split(';'); | |
for(var i = 0; i <ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} | |
// check cookie | |
function checkCookie() { | |
var convert = getCookie("convert"); | |
if (convert != "") { | |
$(submitButton).css("cursor", "not-allowed"); | |
$(label).css("cursor", "not-allowed"); | |
$(field).val(convert); | |
} | |
} | |
checkCookie(); | |
$(field).css("display", "none"); | |
lp.jQuery(function($) { | |
// Config | |
var ruleID = 'alreadySubmitted'; | |
var field = fieldId; | |
var message = errorMessage; | |
var rules = module.lp.form.data.validationRules[field]; | |
$.validator.addMethod(ruleID, function(value, field) { | |
var valid = ( value === '' ); | |
return valid || (!rules.required && !value); | |
}, message); | |
rules[ruleID] = true; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment