...

View Full Version : validate/submit



zoobie
07-13-2003, 04: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>
</form>

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>.

Thanks

beetle
07-13-2003, 06: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.

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

zoobie
07-13-2003, 06: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.

Thanks

beetle
07-13-2003, 06: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!

zoobie
07-13-2003, 06: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.

beetle
07-13-2003, 07: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.

zoobie
07-14-2003, 05: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

Jerome
07-15-2003, 04: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,
Jerome

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

Jerome
07-15-2003, 06:32 PM
Hmmm...

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

jerome

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

function validateForm()
{
if (everythingIsOk)
return true
else
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>

arnyinc
07-15-2003, 09:22 PM
Originally posted by Jerome
Hmmm...

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

jerome

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

zoobie
07-16-2003, 02: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
</form>
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) {
form.submit();
return true;
}
else {
return false;
}
}

and in your link:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum