By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Before Form Submit?

P: n/a
Hello

I am using the following line of code at the very end of my html file to
run a javascript function before a form is submitted.

document.forms[0].onsubmit = chkUsername;

It works for one method, but what do I do when I want to call 2 methods? I
tried this:

document.forms[0].onsubmit = chkUsername;
document.forms[0].onsubmit = chkUsername2;

but the last one is the only one that is run. It seems overwrite the first
one.

Does anyone have any suggestions?

Thanks
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 24 Feb 2004 23:52:31 GMT, Brett Baisley
<ba*****@hotmail.com.REMOVETHIS> wrote:
I am using the following line of code at the very end of my html file to
run a javascript function before a form is submitted.

document.forms[0].onsubmit = chkUsername;

It works for one method, but what do I do when I want to call 2 methods?
I tried this:

document.forms[0].onsubmit = chkUsername;
document.forms[0].onsubmit = chkUsername2;

but the last one is the only one that is run. It seems overwrite the
first one.
It does.
Does anyone have any suggestions?


document.forms[ 0 ].onsubmit = function() {
chkUsername();
chkUsername2();
}

or

document.forms[ 0 ].onsubmit = validateForm;

where validateForm is a function that calls chkUsername() followed by
chkUsername2().

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

P: n/a
Ok, thanks. But if either of the functions the main one class return false,
will it stop?
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Tue, 24 Feb 2004 23:52:31 GMT, Brett Baisley
<ba*****@hotmail.com.REMOVETHIS> wrote:
I am using the following line of code at the very end of my html file to
run a javascript function before a form is submitted.

document.forms[0].onsubmit = chkUsername;

It works for one method, but what do I do when I want to call 2 methods?
I tried this:

document.forms[0].onsubmit = chkUsername;
document.forms[0].onsubmit = chkUsername2;

but the last one is the only one that is run. It seems overwrite the
first one.


It does.
Does anyone have any suggestions?


document.forms[ 0 ].onsubmit = function() {
chkUsername();
chkUsername2();
}

or

document.forms[ 0 ].onsubmit = validateForm;

where validateForm is a function that calls chkUsername() followed by
chkUsername2().

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 20 '05 #3

P: n/a
On Wed, 25 Feb 2004 22:24:16 GMT, Brett Baisley
<ba*****@hotmail.com.REMOVETHIS> wrote:

[snipped top-post]
Ok, thanks. But if either of the functions the main one class return
false, will it stop?


Yes. Take this modified version of an example in my last post:

document.forms[ 0 ].onsubmit = function() {
if( !chkUsername() ) return false;
return chkUsername2();
}

If chkUsername() returns false, the event handler will exit, return false,
and cancel the submission. If it returns true, execution will continue
with the call the chkUsername2(). Whatever the return value from that
function, it will be return it as the function exits. If the value
evaluates to false, the submission will be cancelled.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4

P: n/a
Michael Winter wrote:
On Wed, 25 Feb 2004 22:24:16 GMT, Brett Baisley
<ba*****@hotmail.com.REMOVETHIS> wrote:

[snipped top-post]
Ok, thanks. But if either of the functions the main one class return
false, will it stop?


Yes. Take this modified version of an example in my last post:

document.forms[ 0 ].onsubmit = function() {
if( !chkUsername() ) return false;
return chkUsername2();
}


Unless I'm missing something, what you wrote above could be replaced with:

document.forms[ 0 ].onsubmit = function() {
return chkUsername() && chkUsername2();
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #5

P: n/a
On Fri, 27 Feb 2004 17:38:17 GMT, Grant Wagner
<gw*****@agricoreunited.com> wrote:
Michael Winter wrote:
[snip]
document.forms[ 0 ].onsubmit = function() {
if( !chkUsername() ) return false;
return chkUsername2();
}


Unless I'm missing something, [...]


Nope.
what you wrote above could be replaced with:
I knew there had to be a way to make it smaller (mine seemed unnecessarily
bulky), and your suggestion is that way.
document.forms[ 0 ].onsubmit = function() {
return chkUsername() && chkUsername2();
}


Thank you. :)

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.