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();
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?