View Full Version : validate/submit

07-13-2003, 05:31 AM
I'm using a link to submit...but it's not validating with validate()

<form name="mail" onSubmit="return validate(this);" method="post" action="blah.php">
Form Here
<a href="#" onClick="this.mail.submit();">SEND</a>

I've also tried <a href="#" type="submit">SEND</a> and it seems to submit but doesn't validate.

It works fine just using a submit button <input type="submit" value="SEND">...but I need to submit via the <a href>.


07-13-2003, 07:34 AM
the submit() method for forms does not trigger the onsubmit event.

So, you'll need to peform this via onclick of the link.

Why are you relying on JS to be your submit button? This is generally a Bad Idea&trade;

07-13-2003, 07:41 AM
Yes...The onclick="this.mail.submit()" over-rides the onSubmit...but since I'm using an image swap on hover via the <a href> for the button, I'll have to use this...unless you or anyone else has a better way.

I've tried <a href="#" onClick="if (validate(this)) {this.mail.submit();}" class="button">SEND</a> ...but it needs the "return" in there somewhere.


07-13-2003, 07:46 AM
You can return false to a click event on a link to cancel that click.

Why not use an input type=image? They behave just like submit buttons!

07-13-2003, 07:53 AM
I know...but I'd have to add a bunch of mouseover/out code...and have it load 2 more images...inefficient when I have the a href's hover already set up.

Ugh...Surely, there's a good way to do this...and don't call me Shirley.

07-13-2003, 08:05 AM
I stand by my opinion. It is better to let the HTML handle the critical operation (form submission) and JS handle the non-critical operation (rollover).

It could be done with what you have, and I'll show you only after you seriously consider why you are really doing this, and who it will effect.

07-14-2003, 06:45 AM
Forget it...I just wrote a little function...Sweet

Consider what? A 7% population of coke-bottled, pocket-protected geeks who have turned off their javascript? Not much to consider really...:D

07-15-2003, 05:39 PM
Hi zoobie,

I experience the same problem, and don't want to go the beetle-way this time.

Mine is even more advanced, I think, because the onClick event is located outside the form tags.

Do You have any idea how to solve this?

Thanks in advance,

07-15-2003, 05:53 PM
Call your validate function with the onclick. If it is valid, submit the form from that funciton, otherwise show an error message.

07-15-2003, 07:32 PM

The problem is that I want to validate (this), however my onClick event is located outside the form tags


Roy Sinclair
07-15-2003, 07:56 PM
Here's the basics:

function validateForm()
if (everythingIsOk)
return true
return false;

<a href="#" onclick="(validateForm())? document.forms[0].submit() : alert('You didn't fill out the form properly!');">Submit Form</a>


Of course you could just have the validation function go ahead and submit the form if everything's ok which would simplify the link to:

<a href="#" onclick="validateForm();">Submit Form</a>

07-15-2003, 10:22 PM
Originally posted by Jerome

The problem is that I want to validate (this), however my onClick event is located outside the form tags


If you aren't within form tags, "this" will not reference that form. Reference the form like document.myform.submit().

07-16-2003, 03:52 AM
Yep...You have to name your form then submit like arnyinc said from anywhere:
<form name="fred" action="mail.php" method="post">
Form here
More html
Still more html
<a href="#" onclick="this.fred.submit();">SEND</a>

I'm sure beetle is right...but in some cases, you want to actually use javascript to validate then submit like so:

function validate(form) {
// validate form
if (formIsValid) {
return true;
else {
return false;

and in your link:

<a href="#" onClick="return validate(document.forms[0]);}">SEND</a>