Last active
August 29, 2025 09:34
-
-
Save annuman97/759563b7353bce5df37d4f3ef3f96d60 to your computer and use it in GitHub Desktop.
Custom Fluent Form use case where if required checkboxes are not selected it will won't go to next step and show a validation error message.
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
| (function () { | |
| var FORM_ID = 47; //Your Form ID | |
| function q(form, sel) { return form.querySelector(sel); } | |
| function qa(form, sel) { return form.querySelectorAll(sel); } | |
| function getErrorStack(form) { | |
| return q(form, '#fluentform_' + FORM_ID + '_errors'); | |
| } | |
| function getPeriodsGroup(form) { | |
| var any = q(form, 'input[type="checkbox"][data-name="checkbox"]'); | |
| return any ? (any.closest ? any.closest('.ff-el-group') : null) : null; | |
| } | |
| function clearFieldError(form) { | |
| var group = getPeriodsGroup(form); | |
| if (!group) return; | |
| group.removeAttribute('aria-invalid'); | |
| if (group.classList) group.classList.remove('ff-has-error'); | |
| var msg = group.querySelector('.ff_error_message.ff-custom'); | |
| if (msg && msg.parentNode) msg.parentNode.removeChild(msg); | |
| var stack = getErrorStack(form); | |
| if (stack) { | |
| stack.style.color = ''; | |
| stack.style.fontWeight = ''; | |
| stack.style.fontStyle = ''; | |
| stack.style.marginTop = ''; | |
| stack.textContent = ''; | |
| } | |
| } | |
| function showFieldError(form, msgText) { | |
| var group = getPeriodsGroup(form); | |
| if (!group) return; | |
| group.setAttribute('aria-invalid', 'true'); | |
| if (group.classList) group.classList.add('ff-has-error'); | |
| var content = group.querySelector('.ff-el-input--content') || group; | |
| var msg = group.querySelector('.ff_error_message.ff-custom'); | |
| if (!msg) { | |
| msg = document.createElement('div'); | |
| msg.className = 'ff_error_message ff-custom'; | |
| msg.setAttribute('role', 'alert'); | |
| //Erro Message Style | |
| msg.style.color = 'red'; | |
| msg.style.fontWeight = '400'; | |
| msg.style.marginTop = '12px'; | |
| msg.style.fontStyle = 'italic'; | |
| content.appendChild(msg); | |
| } | |
| msg.textContent = msgText; | |
| var stack = getErrorStack(form); | |
| if (stack) { | |
| stack.style.color = 'red'; | |
| stack.style.fontWeight = '400'; | |
| stack.style.fontStyle = 'italic'; | |
| stack.style.marginTop = '12px'; | |
| stack.textContent = msgText; | |
| } | |
| var firstCb = q(form, 'input[type="checkbox"][data-name="checkbox"]'); | |
| if (firstCb) { try { firstCb.focus({ preventScroll: false }); } catch(e) {} } | |
| } | |
| function isPackageKSelected(form) { | |
| var pk = q(form, 'input[type="radio"][name="payment_input_2"]:checked'); | |
| if (!pk) return false; | |
| var val = (pk.value || '').toUpperCase().trim(); | |
| return val.indexOf('PACKAGE K (30 WEEKS)') !== -1; | |
| } | |
| function countCheckedPeriods(form) { | |
| var boxes = qa(form, 'input[type="checkbox"][data-name="checkbox"]'); | |
| var i, count = 0; | |
| for (i = 0; i < boxes.length; i++) if (boxes[i].checked) count++; | |
| return count; | |
| } | |
| function validatePeriods(form) { | |
| if (!isPackageKSelected(form)) { | |
| clearFieldError(form); | |
| return true; | |
| } | |
| var total = countCheckedPeriods(form); | |
| if (total === 3) { | |
| clearFieldError(form); | |
| return true; | |
| } | |
| //Error Message | |
| showFieldError(form, 'Please select exactly 3 periods to continue.'); | |
| return false; | |
| } | |
| function bindValidation() { | |
| var form = document.getElementById('fluentform_' + FORM_ID); | |
| if (!form) return; | |
| form.addEventListener('click', function (e) { | |
| var btn = e.target && e.target.closest ? e.target.closest('button[data-action="next"]') : null; | |
| if (!btn) return; | |
| var inActiveStep = !!form.querySelector('.fluentform-step.active'); | |
| if (!inActiveStep) return; | |
| if (!validatePeriods(form)) { | |
| e.stopImmediatePropagation && e.stopImmediatePropagation(); | |
| e.preventDefault(); | |
| } | |
| }, true); | |
| form.addEventListener('change', function (e) { | |
| var t = e.target; | |
| if (!t || !t.matches) return; | |
| if ( | |
| t.matches('input[type="radio"][name="payment_input_2"]') || | |
| t.matches('input[type="checkbox"][data-name="checkbox"]') | |
| ) { | |
| validatePeriods(form); | |
| } | |
| }); | |
| clearFieldError(form); | |
| } | |
| if (document.readyState === 'loading') { | |
| document.addEventListener('DOMContentLoaded', bindValidation); | |
| } else { | |
| bindValidation(); | |
| } | |
| window.addEventListener('load', bindValidation); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment