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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation 1

Status
Not open for further replies.

JazzLeg

Programmer
Aug 22, 2002
63
GB
I'm using some javascript form validation in the on_click property of a button. This calls a function. If the function returns false then the page still gets submitted regardless. How can I stop this happening?

Many Thanks
 
why not post some code? one thing that I did notice is that you said:
>>I'm using some javascript form validation in the on_click property of a button

the onclick property should be written as
Code:
<BUTTON onClick
not as
Code:
<BUTTON on_click
but perhaps you put it correctly in your code?
 
try something like this, using return instead of &quot;return false&quot;

function myFunction(){
if(something){
location='nextpage.htm'
}
else return;
}



 
thanks for your replies, this is the code that i'm using, i found it on this forum.

It allows to have two different submit buttons which post to different pages. I added some validation, (which seems to work) but when 'oktosubmit' is set to false, the page just posts itself with all the form data. This is a problem as the page is already created using posted data from the previous page. I just need it to only post when 'oktosubmit' is true.

Code:
<script>
function mysubmitfunction(objButton) {
 var oktosubmit = true;
 // do some client checking here if all the form elements contain valid data.
 // a form element can be addressed like this:
 // eval('myform.myelement').value
 // if you cannot submit because some data in the form is not valid
 // you have to set the oktosubmit to false
 if(oktosubmit) {
    if(objButton.name==&quot;sub1&quot;) {
        myform.action='sub1.asp';
    }
    else {
        myform.action='sub2.asp';
    }
    myform.submit();
 }
 else {
// to prevent the user to click on the submit button twice really fast and submit the page twice
// the button was disabled but because the form cannot be
// submitted we have to tell the user to correct his/her input 
// and give the user the opportunity to submit again.
    objButton.enabled = true;
    return false;
 }
}
</script>
<form name=myform id=myform>
  <input type=text>
  <input type=button onclick=&quot;mysubmitfunction(this);&quot; name=&quot;sub1&quot; value=&quot;submit1&quot;>
  <input type=button onclick=&quot;mysubmitfunction(this);&quot; name=&quot;sub2&quot; value=&quot;submit2&quot;>
</form>
 
replace the onclick=&quot;mysubmitfunction(this);&quot; by onclick=&quot;return mysubmitfunction(this);&quot; for your function return value to be taken in account. Water is not bad as long as it stays out human body ;-)
 
I'm afraid that none of the above posts are completely correct.
This is the right way in most general case:

<form ... onSubmit=&quot;return validateFunction()&quot;>
. . .
<input type=submit>
</form>

No buttons: type=submit should do this.
Also, no any event handlers should be called on [Submit] button click - form onSubmit event is responsible for this.

The function itself should be like this:

function validateFunction() {
// if any error found
return false
// if everything OK
return true
}
 
Thanks guys, I tried targol's advice and it worked. The form only submits now when 'oktosubmit' is true.
Cheers
 
Hi, I was reading this thread with interest. What I'm trying to do is, upon the user pressing the Submit button, have the original page stay on the screen for about 2 seconds before going to the &quot;action&quot; page.

The reason for this is that I want the user to see the text boxes that he left blank populated with their default values.

For example, on submit, I'm having the &quot;End Date&quot; text box populated with today's date if the user left it blank, and I want the user to see this before the &quot;action&quot; page is brought up.

Any ideas?

Thanks & regards,
James
 
Add this to your script :

in the script but outside any function (global var)
Code:
var oInterval;

at the end of your validate function
Code:
oInterval=window.setInterval(&quot;stopTimer()&quot;, 2000);
note that the 2000 value represents 2000 milliseconds.

new function
Code:
function stopTimer() {
  window.clearInterval(oInterval);
  document.form.submit();
}
where
Code:
form
is the id of your form. Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top