PDA

View Full Version : How do i stop the loading of a page with JS?


FatalBreeze
02-24-2008, 12:40 PM
Hello!

I've built a form, and in the form there are some inputs.
So, i've built a little function in JavaScript to validate the value of the fields (in this case a radio button):


function checkShipment()
{
if ((document.shipment.Stype[0].checked) || (document.shipment.Stype[1].checked))
return true;
alert("You didn't Fill The Shipment Information!");
return false;
}


And in the HTML part:

<input type="submit" value="Checkout" onclick="checkShipment()">


Now the problem is this, when the submit button is clicked, and the value of the radio button is neither one of the choices, then the alert apperas, but the new page loads, and it advances to the next page.

I want to know, how can i stop the browser from proccessing to the next page in case of errors. Thanks!

FatalBreeze
02-27-2008, 08:43 AM
please someone?

Dismounted
02-27-2008, 10:00 AM
Use the "onsubmit" property on the form tag and call your function with "return" (ie. onsubmit="return yourcheck(datainput)"). Return false (in your function) to stop submission, true to continue.