Jump to content

confirm alert and checkbox validation


vinpkl

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.