Javascript.js 881 Bytes
Newer Older
1
(function() {
2
    const form = document.querySelector('#agarraunolaputa');
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    const checkboxes = form.querySelectorAll('input[type=checkbox]');
    const checkboxLength = checkboxes.length;
    const firstCheckbox = checkboxLength > 0 ? checkboxes[0] : null;

    function init() {
        if (firstCheckbox) {
            for (let i = 0; i < checkboxLength; i++) {
                checkboxes[i].addEventListener('change', checkValidity);
            }

            checkValidity();
        }
    }

    function isChecked() {
        for (let i = 0; i < checkboxLength; i++) {
            if (checkboxes[i].checked) return true;
        }
        return false;
    }

    function checkValidity() {
25
        const errorMessage = !isChecked() ? 'Debe seleccionar al menos un lenguaje que conozca' : '';
26 27 28 29
        firstCheckbox.setCustomValidity(errorMessage);
    }
    init();
})();