Javascript stopping on error after alert

Posted on 2004-10-27
Last Modified: 2006-11-17
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,
Question by:mjacobs2929
    LVL 13

    Expert Comment

    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();">

    Author Comment


    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 ...

    LVL 13

    Accepted Solution

    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.

    Author Comment

    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,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now