0

I have a from in which, some checkboxes and related that checkboxes, input fields is present. If i select any checkbox then inputfield appears and then I have to fill that input field. If do not fill input field, which checkbox is checked then required message should be show.

My code is:

  <form id="submit_form" action="#" method="POST" autocomplete="off">
  <?php 
  $result = $obj->show_social_icons();
  foreach($result as $key => $row)
  {
  ?>
   <div class="form-group col-md-12 col-sm-12 col-xs-12 heightauto">
    <input type="checkbox" class="maxtickets_enable_cb" <?php if(isset($_SESSION['checkbox']) && ($_SESSION['checkbox'] != '')) { if(in_array($row['id'],$_SESSION['checkbox'])) {?> checked="checked" <?php } else{ echo ""; } }?> name="opwp_wootickets[]" value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?>

     <div class="max_tickets">
      <input type="text" name="link[]" value="<?php if(isset($_SESSION['link']) && ($_SESSION['link'] != '')) { echo $_SESSION['link'][$key]; } ?>" placeholder="<?php echo $row['placeholder']; ?>" class="form-control validatethis">
  </div>
  </div>
  <?php } ?>
   <button type="submit" href="#" name="social_submit" class="next_button">Preview</button>
   </form>

jquery is:

 $('form#submit_form').on('submit', function(event) {
$('.validatethis').each(function() {
    $(this).rules("add", 
        {
            required: true,
            messages: {
                required: "This field is required",
            }
        });
});
});
$("#submit_form").validate();

enter image description here

Validation applied only on first row. In above code, if I filled first checkbox input field and again check other checkbox and empty that inputfield then form submits. How to apply validation in all checked checkboxes input field?

Sparky
  • 98,165
  • 25
  • 199
  • 285
vinod
  • 23
  • 5
  • Show the rendered HTML. This plugin depends on the markup to be constructed correctly. How are we supposed to replicate the JavaScript without the actual markup your JavaScript is using? – Sparky Jul 25 '20 at 14:13
  • FYI - the plugin depends on the `name` being unique. Validation only on first row means that your names are the same on all the rows. The plugin needs `name[0]`, `name[1]`, etc. and cannot handle `name[]` array without the index key. In your PHP loop, insert an index key into the array. – Sparky Jul 25 '20 at 14:15
  • Finally, adding the rules within a submit event handler makes no sense. Add them immediately after the fields are created. – Sparky Jul 25 '20 at 14:21
  • @Sparky Thankyou for reply. But I am confused how to do that. Can U please share some code? – vinod Jul 25 '20 at 14:28
  • Please click on the link for duplicate above your question. – Sparky Jul 25 '20 at 15:33

0 Answers0