דילוג לתוכן
(function(){
const validationMessage = 'נא לאשר את מדיניות האתר'; // your message
// find a checkbox in a form by matching label text fragment
function findCheckboxByLabelText(form, textFragment) {
const labels = Array.from(form.querySelectorAll('label'));
for (const lbl of labels) {
const txt = (lbl.textContent || '').trim();
if (txt && txt.includes(textFragment)) {
// case 1: label wraps the input
const innerCb = lbl.querySelector('input[type="checkbox"]');
if (innerCb) return innerCb;
// case 2: label uses for="id"
const forId = lbl.getAttribute('for');
if (forId) {
const byId = form.querySelector('#' + CSS.escape(forId));
if (byId && byId.type === 'checkbox') return byId;
}
}
}
return null;
}
function attachToForms() {
const allForms = Array.from(document.querySelectorAll('form'));
allForms.forEach(form => {
// try to find the checkbox by short Hebrew fragment "נא לאשר"
const cb = findCheckboxByLabelText(form, 'נא לאשר') || findCheckboxByLabelText(form, 'מדיניות') || form.querySelector('input[type="checkbox"].agree, input[type="checkbox"][name*="agree"], input[type="checkbox"][name*="consent"]');
if (!cb) return;
// Mark required and add handlers
cb.setAttribute('required','required');
// on submit, show custom message / prevent submission if unchecked
const onSubmit = function(e){
if (!cb.checked) {
e.preventDefault();
cb.setCustomValidity(validationMessage);
if (typeof cb.reportValidity === 'function') {
cb.reportValidity(); // show browser bubble in modern browsers
} else {
alert(validationMessage); // fallback
}
// clear custom validity shortly after so next submit can re-check
setTimeout(() => cb.setCustomValidity(''), 2000);
return false;
} else {
cb.setCustomValidity('');
}
};
form.addEventListener('submit', onSubmit, {passive:false});
cb.addEventListener('change', () => cb.setCustomValidity(''));
});
}
function init() {
attachToForms();
// observe DOM in case the form is inserted later (AJAX, widgets)
const mo = new MutationObserver((mutations) => {
attachToForms();
});
mo.observe(document.body, {childList:true, subtree:true});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();