keeping to current page after form validation alert

B

Bill

Hi.

I have a multi-page form where the user enters datails to be submitted to a
database on the final page.

To move through the pages I have Next and Previous buttons using the
following function

function nextPage(loc) {
validateForm('section'); //checks for blank fields and displays an alert
box
location.href=loc +
generateSearchStringFromStore(store); //passes escaped data string made
up from the form entries
} //to the
location in the loc parameter.


This function is called when the Next button is clicked -

<img src="images/Next-button.gif" width="90" height="32" border="0"
align="absmiddle"
onClick="nextPage('page2.html')">

Thing is, blank fields cause validateForm to display an alert box, but when
the user clciks OK page2 is displayed.

I read that if you want to stop the next page loading you have to add the
validation function to the onSubmit listener, so I tried this

<form name="section" id="section" onSubmit="validateForm('section')">

and changed the button handler to this

<img src="images/Next-Button.gif" width="90" height="32" align="absmiddle"
onClick="document.section.submit();
location.href='page2.html' +
generateSearchStringFromStore(store)">

There are no errors reported but nothing at all happens: no alert box
reporting empty fields and page2 doesn't load.

Anyone know what I'm doing wrong?

I;ve seen examples where people write something like

onSubmit="return validateForm('section')"

so I also tried returning a false value from validateForm but nothing
happened then either.

Perplexed....
 
S

steve stevo

When you call the function onsubmit - you are not telling the form when
not to submit

try

onSubmit="return validateForm('section')"

and add

return true/false

within the validation function

Hope this helps

Simon Christie
 
B

Bill

Actually, all I did was

function nextPage(loc) {
if (validateForm('section')) {
updateStoreFromForm('section', store);
location.href=loc +
generateSearchStringFromStore(store);
}
}

and it worked :eek:P

But anyway, what you're saying is that onSubmit="return
validateForm('section')" will stop the submit if the function returns
false, and will allow the submit if true is returned?

I tried something like that earlier but must have mucked it up somehow - but
that's why people put a return in there....?

Thanks.
 
T

Thomas 'PointedEars' Lahn

Bill said:
But anyway, what you're saying is that onSubmit="return
validateForm('section')" will stop the submit if the function returns
false, and will allow the submit if true is returned?

Yes, if JavaScript is supported.
[...] that's why people put a return in there....?
Yes.

[Fullquote]

Please don't make top postings, thanks.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top