简体   繁体   中英

how to validate checkbox and radio button in jquery

I am unable to validate the checkbox and radio button. Actually i just need to insert validation for user at least select one thing weather checkbox or radio. see my code. that's not working for me.

<script>
  $(".submit").click(function(){
       var checkBoxCount = $(".jobwork:checkbox:checked").length;
      if($(".jobwork:checkbox:checked").length < 1 || ($("input[name=jobwork]:checked").length <= 0)){
          alert("Please select atlease one jobwork");
          return false;

      };

  })

http://api.jquery.com/prop/#entry-examples

$(".submit").click(function(){
      var isChecked= $(".jobwork")[0].prop('checked');
      if (!isChecked) {
          alert("Please select atlease one jobwork");
          return false;
      };

  })

You have to check whether the checkboxes has a prop called checked in them. It is available only if the checkbox is checked.

 $(".submit").click(function(){ var checked = $(".jobwork").prop('checked'); if(!checked){ alert("Please select atlease one jobwork"); return false; }; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="jobwork" name="check"> <input type="checkbox" class="jobwork" name="check"> <input type="checkbox" class="jobwork" name="check"> <br> <input type="button" class="submit" value="submit"> 

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