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

preventing form submission for 4

Status
Not open for further replies.

travisbrown

Technical User
Joined
Dec 31, 2001
Messages
1,016
When a user clicks submit and a checkbox isn't checked, the page executes the apert below. Problem is, form still submits when you "ok" the alert. How do only submit the form when the checkbox is checked?

Code:
	function checklegal() {
		if( !document.forms.legal.agree.checked ) {
		alert("Please agree to continue");
		}
	};

exexuted from onsubmit.
 
update your function to include a "return false" as such:

Code:
 function checklegal() {
        if( !document.forms.legal.agree.checked ) {
        alert("Please agree to continue");
        [!]return false;[/!]
        }
    };

Then, in the FORM tag, include a "return" prior to the function call in the ONSUBMIT event:

Code:
<form onsubmit='[!]return[/!] checklegal()'>

Now, if checklegal() returns false, the onsubmit returns that to the form and prevents submission.

'hope that helps.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Your call to the function in your form tag should be something like:
onsubmit="return checklegal()"

and your function should return false if there is an error
Code:
function checklegal() {
        if( !document.forms.legal.agree.checked ) {
          alert("Please agree to continue");
          return false;
        }
    };

Stamp out, eliminate and abolish redundancy!
 
Deep reduction to get it is this.
[tt] <form onsubmit="return checklegal();">[/tt]
and
[tt]function checklegal() {
if( !document.forms.legal.agree.checked ) {
return confirm("Please agree to continue");
}
};[/tt]
 
Try this:
Code:
function checklegal()
{
if( !document.forms.legal.agree.checked )
  {
  alert("Please agree to continue");
  [red][b]return false[/b][/red];
  }
return true;
}

Lee
 
Disregard mine. It is mistaken.
 
Cool. I had tried return false already, but used else return false. Also needed to also put in return true to get the form to actually submit if it was checked, else the function would perpetually return false.

Trollacious caught this too.

thanks all.
 
Actually, it would only "perpetually return false" if you perpetually didn't check your check box. If the code doesn't enter the if-block, then, by default, it should return 'true' (even without you explicitly stating it).

What you DO need is the 'return' in the onsubmit statement in your FORM tag. (see above)

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Yeah, I always had return in the event handler.

Maybe I had a typo or the js was still cached, but when I first cut and pasted your code, the form stood still when the checkbox was checked. Probably when I superfluously added the return true, I refreshed the page and it worked.

Trimmed return true out now.

Thanks all.
 
Well, you don't have to take it OUT. Leaving it in leaves it clearer for the person who might want to study and learn from your code.

I was just whoring for a star! Thanks! :-D

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Well yeah, but if I don't need return true in the function, then why?

Stars for the deserved, starwhore. ;)
 
Damn, I am just not whorish enough I guess.
Same answer posted within the same minute as LFI and a whole minute before trollacious (who got the FIRST star I might add).

I just aint gettin' no lovin these days. I'm going back to my own code for a while.


Stamp out, eliminate and abolish redundancy!
 
And you're still above both of them on the MVP list [wink]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
OK that brings back my warm and fuzzy feeling.
I do not need stars, I am benefiting greatly enough already from what I learn as I read and assist.

It is amazing how improved my own coding has become since I joined these forums.

But if I do another 30+ post with someone involving a few hundred plus lines of code and they do not say thank you can we have em booted off the site?



Stamp out, eliminate and abolish redundancy!
 
You got my vote on that! I generally don't even read posts with more than a couple dozen lines of code because it often indicates that the poster doesn't understand what's relevant to the question and what isn't, or what the problem really is.

Lee
 
Yeah, but I thought you guys were wrong first, which is why Trollacious got first star.

Handing out stars like E at a discotheque...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top