Emily
Emily

Reputation: 33

How to set required field in contact form 7 wordpress

I have a list of multiple checkboxes in the form. The user can check all the checkboxes but at least one is required. Without selecting at least one, checkbox they should not be able to submit the form. how to make it in contact form 7? I have added the script which is not working.

Here is my script

$(document).ready(function () { $('#checkBtn').click(function() {

checked = $("input[type=checkbox]:checked").length;

 if(!checked) {
    alert("You must check at least one checkbox.");
    return false;
  }

});

});

Upvotes: 1

Views: 9648

Answers (3)

Trace
Trace

Reputation: 29

Paste the code from above on the bottom of the form instead of the Additional Settings

$('#fm_submit').submit(function(e){
e.preventDefault();
var ck_box = $('input[type="checkbox"]:checked').length;
// return in firefox or chrome console the number of checkbox checked enter code here
console.log(ck_box); 
if(ck_box < 1){ alert('Select any one box'); } 
});

Upvotes: 1

Manthan Kanpariya
Manthan Kanpariya

Reputation: 104

It is perfectly work. On this set your code

$('#fm_submit').submit(function(e){
    e.preventDefault();
    var ck_box = $('input[type="checkbox"]:checked').length;
    
    // return in firefox or chrome console 
    // the number of checkbox checked
    console.log(ck_box); 

    if(ck_box < 1){
      alert('Select any one box');
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name = "frmTest[]" id="fm_submit">
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" value="true" checked="true" >
  <input type="checkbox" >
  <input type="checkbox" >
  <input type="submit" id="fm_submit" name="fm_submit" value="Submit">
</form>
<div class="container"></div>

Upvotes: 1

Manthan Kanpariya
Manthan Kanpariya

Reputation: 104

In this only write star(*) after checkbox element in the plugin form

 [checkbox* checkbox "test 1" "test 2"]

 [submit "Send"]

Upvotes: 1

Related Questions