Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation before submitting Form

Status
Not open for further replies.

Pinpoint

Programmer
Dec 23, 2001
49
GB
Hi,

I'm trying to check the contents of an HTML form using the onSubmit="return Validate();" option within the form statement, where Validate is a JS function. The function seems to be called okay before the Submit button, however whatever the result returned from Validate(), the Submit processing is then actioned, i.e. in the case of a validation error it is ignoring the 'false' return-code coming out of the function.

<form name=&quot;ViewDetails&quot; action=&quot;NextScreen&quot; method=POST
onSubmit=&quot;return Validate();&quot;>

<input type=&quot;submit&quot; value=&quot;Confirm Details&quot;>
</form>

Any ideas on getting round this ?

Thanks,

Pinpoint
 
Here's a bit of code that I used for a business site. It's part of a questionnaire. The javascript file is external to the page and is referenced in the page like this:
<script src=&quot;efnww.js&quot; type=&quot;text/javascript&quot;></script>

function questionnaire(qform){
if(qform.bus_name.value.length == 0){
alert(&quot;Enter your FULL BUSINESS NAME&quot;);
qform.ques1[0].focus();
qform.bus_name.focus();
return (false);}

if ((qform.bus_address.value.length + qform.bus_address1.value.length) == 0){
alert(&quot;Enter your BUSINESS ADDRESS&quot;);
qform.bus_name.focus();
qform.bus_address.focus();
return (false);}

if (qform.city.value.length == 0){
alert(&quot;Enter the CITY of your mailing address&quot;);
qform.bus_address.focus();
qform.city.focus();
return (false);}
}


The form tag is like this:

<form method=&quot;post&quot; action=&quot; name=&quot;theForm&quot; onSubmit=&quot;return questionnaire(this)&quot;>

This works every time, unless, of course, the user has disabled javascript. I also do the same validation in the cgi script in case this happens.

Maybe you could post a link to your page and we could take a look at it?

There's always a better way. The fun is trying to find it!
 
Thanks for your help guys. I have now got the coding to work okay, without any changes. Looks like it was a problem with my test rig. The Form is infact sent for processing to a server-side C++ Shopping cart app, so trying to simulate that in a test situation on my local PC must have caused the dodgy response.

Pinpoint.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top