Skip to content

Instantly share code, notes, and snippets.

@DuaneNielsen
Created December 12, 2020 20:37
Show Gist options
  • Save DuaneNielsen/78a25225bcb125bfd009ea877f218f20 to your computer and use it in GitHub Desktop.
Save DuaneNielsen/78a25225bcb125bfd009ea877f218f20 to your computer and use it in GitHub Desktop.
Hubspot Form Validation
http://dev.jackcoldrick.com/custom-form-validation/
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "2990812",
formId: "c7782001-207b-408b-b009-4a57afed6056",
onFormReady: function($form, e) {
document.querySelector("[type='submit']").addEventListener("click", function(event) {
//if competition code doesn't contain 2 letters and 4 numbers then stop the form from submitting...
var code = $('input[name="competition_code"]').val();
var pattern = new RegExp('^MWL[0-9]{6}$', 'i');
if (pattern.test(code)) {
console.log(code + " is valid");
} else {
event.preventDefault();
console.log(code + " is not valid");
$('.alert').show();
$('.userinput').html(code);
}
}, false);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment