Last active
August 20, 2021 22:06
-
-
Save dmitrybndar/8cd93a09288985c4a9fcae97856fcb1d to your computer and use it in GitHub Desktop.
Hubspot Form validation function
This file contains hidden or 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
| hbspt.forms.create({ | |
| portalId: 'PORTAL_ID', | |
| formId: 'FORM_ID', | |
| onFormReady: function($form) { | |
| var $formRequiredElements = $form.find('[required]'); | |
| var formRequiredFields = []; | |
| $formRequiredElements.each(function() { | |
| var fieldName = $(this).attr('name'); | |
| if (!fieldName) { | |
| fieldName = $(this) | |
| .find('[name]') | |
| .attr('name'); | |
| } | |
| formRequiredFields.push(fieldName); | |
| }); | |
| function validateForm() { | |
| var fields = $(this).serializeArray(); | |
| var isValid = true; | |
| formRequiredFields.forEach(function(fieldName) { | |
| var field = fields.filter(function(field) { | |
| return field.name === fieldName; | |
| })[0]; | |
| if (!field || field.value === undefined || field.value === '') { | |
| isValid = false; | |
| } | |
| }); | |
| $form.find('input[type="submit"]').prop('disabled', !isValid); | |
| } | |
| validateForm(); | |
| $form.on('change, input', validateForm); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment