Javascript stopping on error after alert

How can I keep this from reloading the page after the user clicks the alert? I want the user to see an image next to the offending form field, which it is doing, but then immediately reloads the page:

if ( (adults + seniors) < infants ){
alert("The number of infants in a group cannot be \nmore than the number of adults or senior citizens.")
obj = document.getElementById("passInfant");
      spanObj = document.getElementById("errorImage");
      spanObj.innerHTML = "<img src='img/error_star.gif'>";
return false

Many thanks,
Who is Participating?
you say when the form is submitted?

Then may I suggest removing it from the onclick event of the submit button, and instead placing it in the onsubmit event of the form, along with the 'return' statement?

This is how I always validate my forms:

<form ... onsubmit="return submit_FC_Form();">

The reason I say to do this is because submit_FC_Form() will return a value: true or false.  However, regardless of this value, the submission will still occur.  You need to instead return to the onsubmit event the value that submit_FC_Form() returns.  If the function itself returns false (an error was found) then the form will not submit.
How is this function being called?

You might need to return the value of this function to the actual event that's calling it.
For example:

<blah onclick="return functionName();">
mjacobs2929Author Commented:

The code above is a snippet from a function called checkPassenger() .

checkPassenger() is being called from within another function when the form is submitted:

submit_FC_Form() calls quite a few other functions before finally submitting the form:

function submit_FC_Form(){
 ... other checks ... then...

      if (checkCities() && checkDate() && checkPassenger() && enforceMaxPax() && finalDate() )

 ... more stuff ...

mjacobs2929Author Commented:
That works if I use:
<form ... onsubmit="checkPassenger();">

but is still submitting if I use:
<form ... onsubmit="return submit_FC_Form();">

So I guess I need to re-write submit_FC_Form()


Many thanks,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.