Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript stopping on error after alert

Posted on 2004-10-27
4
Medium Priority
?
185 Views
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,
MJ
0
Comment
Question by:mjacobs2929
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12422838
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();">
0
 

Author Comment

by:mjacobs2929
ID: 12422943

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

checkPassenger() is being called from within another function when the form is submitted:
onclick="submit_FC_Form()"

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


0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 375 total points
ID: 12422977
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.
0
 

Author Comment

by:mjacobs2929
ID: 12423143
Thanks.
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,
MJ

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

636 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