jquery - Validate dynamically created array values using Javascript in two different ways -
this question has answer here:
here html code:
<tr> <td colspan="3" class="table-success"> <div class="ck"> <?php echo $sno=$sno+1; ?><input type="hidden" id="sno" value="<?php echo $sno;?>"/> </td> <td colspan="3" class="table-success"> <div class="ck"> <input readonly name="emp_code[]" id="emp_code[]" value="<?php echo $row['emp_code']?>" class="form-control"> </td> <td colspan="3" class="table-success"> <div class="ck"> <input readonly name="emp_name[]" id="emp_name_<?php echo $sno;?>" value="<?php echo $row['emp_name']?>" class="form-control"> </td> <td colspan="3" class="table-info"> <div class="ck"> <input readonly name="emp_mailid[]" id="emp_mailid_<?php echo $sno;?>" value="<?php echo $row['emp_mailid']?>" class="form-control"> </td> <td colspan="3" class="table-info"> <div class="ck"> <input readonly name="nomination[]" id="nomination_<?php echo $sno;?>" value="<?php echo $row['nomination']?>" class="form-control"> </td> <td colspan="3" class="table-info"> <div class="ck"> <label><input type="radio" name="attendance[<?php echo $nextrowid ?>]" id="attendance[<?php echo $nextrowid ?>]" value="1"/>yes</label> <label><input type="radio" name="attendance[<?php echo $nextrowid ?>]" id="attendance[<?php echo $nextrowid ?>]" value="0">no</label> </td> </tr>
here javascript code:
<script type="text/javascript"> //function add new row jquery(function() { var counter = 1; var nextrowid=($('#tb tr').length)-1; //number of rows fetched database jquery('a.addrow').click(function(event) { event.preventdefault(); var newrow = jquery('<tr><td colspan="3" class="table-active">'+nextrowid+'</td><td colspan="3" class="table-active"><input type="text" name="emp_code[]" id="emp_code[]" class="form-control"/></td><td colspan="3" class="table-success"><input type="text" name="emp_name[' + nextrowid + ']" id="emp_name[' + nextrowid + ']" class="form-control" required/></td><td colspan="3" class="table-info"><input type="text" name="emp_mailid[' + nextrowid + ']" id="emp_mailid[' + nextrowid + ']" class="form-control" required/></td><td colspan="3" class="table-info"><select required class="form-control" id="nomination[' + nextrowid + ']" name="nomination[' + nextrowid + ']"> <option value="">select</option><option value="nominated">nominated</option><option value="invited">invited</option><option value="both">both</option><option value="none">neither nominated nor invited</option></select></td><td colspan="3"><label><input type="radio" name="attendance[' + nextrowid + ']" id="attendance[' + nextrowid + ']" value="1" required>yes</label><label><input type="radio" name="attendance[' + nextrowid + ']" id="attendance[' + nextrowid + ']" value="0">no</label></td><td><a href="javascript:void(0);" class="remove" onclick=><span class="glyphicon glyphicon-remove"></span></a></td></tr>'); counter++; nextrowid++; jquery('table.table').append(newrow); j++; //init bootstraptoggle $('.toggleone').bootstraptoggle({ on: 'yes', off: 'no' }); }); }); </script>
this function called when button clicked add new row want validate whole array i.e emp_code[0]....emp_code[n]
, when used validation code doesn't work. here jquery:
<script type="text/javascript"> $().ready(function () { // validate signup form on keyup , submit $("#form1").validate({ rules: { training_date: "required", from_time:"required", to_time:"required", venue:"required", 'emp_code[]': { required: true, } }, messages: { }, submithandler: function (form) { // demo alert('valid form'); return false; } }); }); </script>
i need validate complete array using form validation in jquery. there many questions similar differs same array name e.g. emp_code[]
stores values in ordered manner, whereas previous questions don't. ii tried answers nothing worked out, came question.
Comments
Post a Comment