WordPress.org

Support

Support » How-To and Troubleshooting » WP Login Form Code not getting validated, while adding a Checkbox

WP Login Form Code not getting validated, while adding a Checkbox

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ah, here’s your code.

    I think if it were me I would give the checkbox an ID and change how you’re checking the state of the checkbox so that you’re using the boolean value of .prop. Say your checkbox has an ID of “mycheckbox:”

    $('#form1').submit(function() {
    if ($('#mycheckbox').prop('checked')) {
    // everything's fine...
    } else {
    alert('Please select something!');
    return false;
    }
    });

    If you have a whole bunch of checkboxes, and you want to make sure at least one is checked, the ID option won’t work, but you can use the combination of a fieldset and an array like this, where your fieldset looks like:

    <fieldset id="checkArray">
        <input type="checkbox" name="chk[]" value="Apples" />
        <input type="checkbox" name="chk[]" value="Bananas" />
    </fieldset>

    and your JQuery would be either:
    var atLeastOneIsChecked = $('#checkArray :checkbox:checked').length > 0;
    or without the fieldset using the array of checkboxes:
    var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;

    Thank you, I figured out my the code wasn’t working was because the Jquery was conflicting with other plugins therefore not being able to execute the code. I found a quick way to be able to execute code, which can be wrapped around the code below:

    jQuery(document).ready(function($) {
        // $() will work as an alias for jQuery() inside of this function
    });

    Appreciate the help though!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP Login Form Code not getting validated, while adding a Checkbox’ is closed to new replies.
Skip to toolbar