dimanche 24 décembre 2017

How to get alert when checkboxes in the table are not clicked and try to submit form

I am trying to send checkbox values to other page to delete the selected rows with checkbox but when there is no checkbox clicked and try to send it should give alert for checkbox is empty and if the form is submitting after the checkbox is clicked then it should give alert whether to proceed or not this is my form

<form action="deleteselectedhr" name="deleteFiles" method="post" onsubmit="checkForm()">    
<table id="mytable" border=3 >
 <c:forEach items="${users}" var="user">
 <tr>
   <td>
     <input type="checkbox" id="saif" name="<c:out value="${user.hrid}" />" 
      value="<c:out value="${user.hrid}" />" />
   </td>  
</tr>
 </c:forEach>
</table>
<input TYPE="SUBMIT" value="Delete Selected HRs"/>
</form>
Javascript:
  <script type="text/javascript">
function checkForm(){

    var checkt = document.getElementsById('saif');
    var chekSelect = false;
    for (var i = 0; i < checkt.length; i++) {
        var myElement = checkt[i];
        if (myElement.type === "checkbox" && myElement.checked) {
            if (myElement.checked) {
                chekSelect = true;
                break;
            }
        }
    }
    if(!chekSelect) {
        alert('Please Check Atleast one record to print cheque!!!');
        return false;
    } else {
        return true;
    }
}
</script>`




Aucun commentaire:

Enregistrer un commentaire