vinpkl Posted June 22, 2009 Share Posted June 22, 2009 hi all i have one form inside other form in which i have delete button and i want to apply validation on it. This is form code <form id="form1" name="form1" method="get" action="" onsubmit='return cbox();'> <? echo "<form name='del_sel' method='post'>"; echo "<td valign='top' colspan='9'><input name='submit' type='submit' value='Delete Selected' onClick=\"return confirm('Are you 100% totally certain that you want to DELETE this ?')\"/>"; echo "<input type='hidden' name='checkbox[]' value=".$row2['order_id']." />"; echo "</td>"; echo "</form>"; ?> </form> this is validation function which checks whether one checkbox is selected or not. function cbox() { var chks = document.getElementsByName('checkbox[]'); var hasChecked = false; for (var i = 0; i < chks.length; i++) { if (chks[i].checked) { hasChecked = true; break; } } if (hasChecked == false) { alert("Please select at least one."); return false; } return true; } now the problem is that cbox() function and confirm function both appears one after another. i want that if cbox() returns true means if any one of the chekbox is selected then only the confirm alert should appear otherwise it should not appear. vineet Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 22, 2009 Share Posted June 22, 2009 You have a FORM within a FORM? That makes no sense. Create just one form with an onsubmit trigger that first checks if any checkboxes are selected then asks for a confirmation. I would also suggest you give your variables/functions descriptive names. "cbox" tells nothng about what the function is or does. And using the name "checkbox" is even worse. what does the checkbox represent? Some item ID I expect, but I would giv it a representative naem, e.g. artistID, orderID, etc. JS Functions function isChecked(checkGroup) { //Iterrate through each checkbox in the group //to ensure at least one is checked for (var i=0; i<checkGroup.length; i++) { if (checkGroup[i].checked) { return true; } } alert("Please select at least one."); return false; } function validateForm(formObj) { //Ensure at least one item is checked if(!isChecked(formObj['item_id[]'])) { return false; } //Confirm the deletion return confirm('Are you 100% totally certain that you want to DELETE this ?'); } HTML (I'm assuming there's supposed to be some kind of loop here for the items?) <FORM ID="form1" NAME="form1" METHOD="get" ACTION="" ONSUBMIT='return validateFOrm(this);'> <TD valign="top" colspan="9"> <INPUT NAME="submit" TYPE="submit" value="Delete Selected" /> <INPUT TYPE="hidden" NAME="item_id[]" VALUE="<?php echo $row2['order_id']; ?>" /> </TD> </form> Quote Link to comment Share on other sites More sharing options...
vinpkl Posted June 22, 2009 Author Share Posted June 22, 2009 hi mjdamato thanks for the code. it works great. i will do the changes also as suggested by you. vineet Quote Link to comment Share on other sites More sharing options...
vinpkl Posted June 22, 2009 Author Share Posted June 22, 2009 hi mjdamato i want to add validation to my <select> options as <select name="choice" id="choice"> <option value="0">select choice</option> <option value="PENDING">Pending orders</option> <option value="PAID">Paid orders</option> </select> <input name="choice_submit" id="choice_submit" type="submit" value="click" onclick="return chose();" /> here is the complete validation <script language="javascript"> function chose() { if(document.form1.choice.value=='0') { alert("choose your option"); return false; } } function isChecked(checkGroup) { //Iterrate through each checkbox in the group //to ensure at least one is checked for (var i=0; i<checkGroup.length; i++) { if (checkGroup[i].checked) { return true; } } alert("Please select at least one."); return false; } function validateForm(formObj) { //Ensure at least one item is checked if(!isChecked(formObj['item[]'])) { return false; } //Confirm the deletion return confirm('Are you 100% totally certain that you want to DELETE this ?'); } </script> this is validation function chose() { if(document.form1.choice.value=='0') { alert("choose your option"); return false; } } But on click of the button it asks for selecting any checkbox instead of showing the result of selected <select> option. Even if i dont apply any validation to <select> box, then also on click of <select> submit button it asks for selecting checkbox. The checkbox validation code is conflicting with the <select> validation code as both are different. how can i rectify it. vineet Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.