Search code examples
javascriptjqueryinputcheckbox

Checkbox and text field, if one is selected or has value require other(s)


I have two checkboxes in a group and one text input. If one (or both) of the checkboxes are selected I need to have the text input be required, as well as if the text input has text I need at least one of the checkboxes to be required. Another problem I'm having it that it's using a custom templating engine (PHP backend) and is a pain to configure and get the attributes correct, another issue is it's all referenced by the name attribute and this is why I'm using a HTML5 data-group for the checkbox options which I think it working.

Any help in getting this to work, combining functions (if this makes it easier/simpler).

BTW it's running 1.3.2 jQuery

Example: (not working)

Any suggestions?

JS:

function checkboxSelectedRequiredAdditionalFields(elem) {
    var passedElement = $('input:checkbox[name=' + elem + ']');

    passedElement.click(function() {
        $('input[name=number]').attr('required', true).append('<span class="required">*</span>');
        alert('text is required now?');
    });
}

function numberEnteredRequiredAdditionalFields(elem) {
    var passedElement = $('input[name=' + elem + ']');

    if (passedElement.val().length > 0) {
        var boxes = $('input[data-group=cbOptions]').click(function() {
            boxes.not(this).attr('required', false);
            alert('checkbox is selected so other checkbox is not required');
        });

        $('input[data-group=cbOptions]').each(function() {
            $(this).attr('required', true).next().append('<span class="required">*</span>');
            alert('checkbox is required now?');
        });
    }
}

HTML

<form>
    <label>
        <input type="checkbox" name="checkbox1" value="t" onclick="checkboxSelectedRequiredAdditionalFields('checkbox1');" data-group="cbOptions">
        Checkbox Option 1
    </label>
    <label>
        <input type="checkbox" name="checkbox2" value="t" onclick="checkboxSelectedRequiredAdditionalFields('checkbox2');" data-group="cbOptions">
        Checkbox Option 2
    </label> 
    Number <b>
    <input type="text" name="number" value="" size="" maxlength="9" onclick="numberEnteredRequiredAdditionalFields('number');">
    </b>
</form>

Solution

  • You should separate the JavaScript from the HTML. Fiddle: http://jsfiddle.net/NYn8e/6/. If possible, remove <b> from the HTML source, and extend the style sheet with the right CSS property: font-weight: bold;.

    <form>
        <label>
            <input type="checkbox" name="checkbox1" value="t" data-required="checkbox">
            Checkbox Option 1
        </label>
        <label>
            <input type="checkbox" name="checkbox2" value="t" data-required="checkbox">
            Checkbox Option 2
        </label>
        Number <b>
        <input type="text" name="number" value="" size="" maxlength="9" data-required="number">
        </b>
    </form>
    

    JavaScript:

    function required(){
        //Any checked checkbox? checked == 0 = no, otherwise: yes
        var checked = $('input[data-required=checkbox]:checked').length;
        var $checkboxes =  $('input[data-required=checkbox]');
        var $num = $('input[name=number]');
        var length = $num.val().length;
    
        //Remove previously added span, if existent.
        $num.next('span.required').remove();
        $checkboxes.next('span.required').remove();
        if(!length && checked){
            $num.after('<span class="required">*</span>');
            alert("Number required!");
        } else if(length && !checked){
            $checkboxes.after('<span class="required">*</span>');
            alert("Check at least one checkbox.");
        }
    }
    $(document).ready(function(){
        $("[data-required]").change(required);
    });