简体   繁体   中英

Check if at least one checkbox is checked

I need to validate to see if at least one checkbox is checked before sending the data to the form sending script. this is what i have so far (to validate all fields the checkbox one is at the bottom)

$('form#ajax_form .submit').click(function () {
    $('#ajax_form .error').hide(); //if error visibile, hide on new click
    var name = $('input#name').val();
    if (name == "" || name == " " || name == "Name") {
        $('input#name').focus().before('<div class="error">Hey, what´s your name...?</div>');
        return false;
    }
    var email_test = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
    var email = $('input#email').val();
    if (email == "" || email == " ") {
        $('input#email').focus().before('<div class="error">Psssst. You missed one...</div>');
        return false;
    } else if (!email_test.test(email)) {
        $('input#email').select().before('<div class="error">Please recheck your email</div>');
        return false;
    }
    var message = $('#message').val();
    if (message == "" || message == " " || message == "Message") {
        $('#message').focus().fadeIn('slow').before('<div class="error">Remember your message!</div>');
        return false;
    }
    if ($("[name=Field]:checked").length > 0) {
        $('#services1').focus().after('<div class="error">Choose at least one please.</div>');
        return false;
    }
    var company = $('input#company').val();
    $.post(rootUrl + '/wp-admin/admin-ajax.php', {
        action: "two2_send_contact_form",
        email: email,
        name: name,
        message: message,
        company: company
    }, function (data) {
        $('form#ajax_form').slideUp('fast').before('<div id="success"></div>');
        $('#success').html(data).slideDown(9000);
    });

and the checkboxes.

 <div style="display:inline;">
    <span>
      <label class="choice" for="Field1" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field1" name="Field[]" type="checkbox" class="field checkbox" value="Web Design" style="width:0px;">Web Design    
 </label>
    </span>
    <span>
      <label class="choice" for="Field2" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field2" name="Field[]" type="checkbox" class="field checkbox" value="Graphic Design" style="width:0px;">Graphic Design
</label>
    </span>
    <span>
      <label class="choice" for="Field3" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field3" name="Field[]" class="field checkbox" type="checkbox" value="Illustration" style="width:0px;">Illustration
 </label>
    </span>
    <span>
      <label class="choice" for="Field4" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field4" name="Field[]" type="checkbox" class="field checkbox" value="Identity Dev/Branding" style="width:0px;">Identity Dev/Branding
</label>
    </span>
    <br>
    <span>
      <label class="choice" for="Field1_3" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field5" name="Field[]" class="field checkbox" type="checkbox" value="Identity Dev/Branding" style="width:0px;">Web App Development
</label>
    </span>
    <span>
      <label class="choice" for="Field1_4" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field6" name="Field[]" class="field checkbox" type="checkbox" value="CGI" style="width:0px;">CGI
</label>
    </span>
    <span>
      <label class="choice" for="Field1_5" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field7" name="Field[]" class="field checkbox" type="checkbox" value="VFX" style="width:0px;">VFX
</label>
    </span>
    <span>
      <label class="choice" for="Field1_6" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field8" name="Field[]" class="field checkbox" type="checkbox" value="Video Production" style="width:0px;">Video Production 
 </label>
    </span>
    <span>
      <label class="choice" for="Field1_7" style="display:inline;margin-left:0px;margin-right:5px;">
        <input id="Field9" name="Field[]" class="field checkbox" type="checkbox" value="Motion Project" style="width:0px;">Motion Project
</label>
    </span>
  </div>
$('input[type="checkbox"]:is("checked")').length > 0 

at least one checkbox is check

With jquery > 1.7 you can call

$('input:checked').length

Change:

if ($("[name=Field]:checked").length > 0) {

To:

if ($("[name='Field[]']:checked").length > 0) {

All of your checkboxes have square brackets in their name attribute name="Field[]" but you were trying to select them without the square brackets as name=Field .

It depends on how you want to achieve this. If you just want to make sure at least one checkbox in all groups is checked then this should be enough:

if ($('input[type=checkbox]:checked').length) {
   // at least one is checked do something
}

In case you want to check if at least one per group is checked then is slightly more complicated. Here's a fiddle I created to answer another question a while ago that might help: http://jsfiddle.net/elclanrs/GFCKA/

In any case, as you're working with more validation elements and inputs, I suggest you take a look at this plugin , it may help you with other stuff too.

For those not using jQuery:

const form = document.getElementById('ajax_form');
const valid = ajax_form.querySelectorAll('input[type="checkbox"]:checked').length;

if (valid > 0) {
  // code block here
}

Otherwise, refer to this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM