Click to See Complete Forum and Search --> : Only submitting form if conditions are true


Arc
08-17-2003, 10:55 AM
Hello, basically I have a validate function that checks an array of dynamically created checkboxes to see if any were checked. If not it raises an alert. If some were selected it raises a confirmation alert asking the user if they are sure they want delete.

I only want the form to submit if the user clicks yes, but I can't figure out how. The form submits no matter what.


Here is the Function I am using.


function Validate(){
var form = document.forms[0]
var counter=0;
for (i = 0; i < form.elements.length ; i++){
if (form.elements[i].type == "checkbox"){
if(form.elements[i].checked == true)counter++
}
}
if (counter == 0){
alert("You have not selected an item to delete");
//Don't submit the form
}else{
if(!confirm("All products associated with these categories will also be deleted.\nAre you sure you want to delete these categories?")){
//Don't submit the form
}
}
}


I have tried it in the forms OnSubmit event and the Buttons OnMouseDown event.

On the forms OnSubmit even It raises the Alerts as it should but it submits the form no matter what you click.

In the Buttons OnMouseDown event it raises the alerts as it should but it doesnt submit the form even if you click yes.

What is the best way to go about this?

Thanks!:D

Charles
08-17-2003, 11:16 AM
To keep the form from submitting, the validation function, which should be called "validate" and not "Validate", needs to return "false". And the FORM's opening tag should contain onsubmit="return validate(this)"

And the first line of your function should read function validate (form) { and you should delete the second line of your function.

Arc
08-17-2003, 11:52 AM
That worked perfectly. Thanks alot!:D