473,242 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Question about which is the correct style of JavaScript to pass to FORM's onSubmit attribute

Which is the following is correct?

a) <form ... onSubmit="return checkData()">

b) <form ... onSubmit="return checkData();">

c) <form ... onSubmit="checkData()">

d) <form ... onSubmit="checkData();">

....where checkData is a JavaScript function that returns a true or false
value.

It seems that Internet Explorer accepts all of the above, but I'm not
taking that to mean that all are correct.

I've attempted to search the W3 web site on the terms form and onsubmit,
but the documentation refers to the attribute value as being an intrinsic
event (without giving any examples). The archived mailing lists contain
messages which demonstrate all of the options that I've provided above. I
gave up after the forth page of search results because of the number of
conflicting examples.

Does anyone know which is correct?

Thanks for your precious time.

Jul 23 '05 #1
2 6451
On Mon, 15 Nov 2004 15:51:19 GMT, Sean Dockery <sd******@securac.net>
wrote:
Which is the following is correct?

a) <form ... onSubmit="return checkData()">

b) <form ... onSubmit="return checkData();">

c) <form ... onSubmit="checkData()">

d) <form ... onSubmit="checkData();">

...where checkData is a JavaScript function that returns a true or false
value.
All of them are correct in that they will result in the call of checkData.
However, only a) and b) will actually affect whether the form is submitted.

The content of an intrinsic event attribute becomes the body of an
anonymous function:

/* Note that IE doesn't pass an event argument. It uses a global
* variable of the same name. Either way, you can still use:
*
* on<event>="myFunction(event)"
*
* to access the event object.
*/
formElement.onsubmit = function(event) {
/* Value in onsubmit attribute */
};

Obviously, if you don't include a return statement, no value will actually
be returned when this anonymous function exits.

With regards to the semicolon, the same rules apply as normal: semicolons
are generally optional, but it's good practice to include them. If you had
two or more statements/expressions to evaluate, a semicolon would be
necessary to separate them.

The final point I like to make is that it's simpler to pass a reference to
the FORM when it is called, rather than obtain one later. That is:

<form ... onsubmit="return checkData(this);">

function checkData(form) {
/* You can now use form, rather than
* document.forms['formName']
*/
}
It seems that Internet Explorer accepts all of the above, but I'm not
taking that to mean that all are correct.


As I said, they are all syntactically correct, but the second two are
probably not what you want.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Thanks, Michael. Your response was extremely helpful, and exactly what I
was looking for.

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opshiklmsxx13kvk@atlantis...
On Mon, 15 Nov 2004 15:51:19 GMT, Sean Dockery <sd******@securac.net>
wrote:
Which is the following is correct?

a) <form ... onSubmit="return checkData()">

b) <form ... onSubmit="return checkData();">

c) <form ... onSubmit="checkData()">

d) <form ... onSubmit="checkData();">

...where checkData is a JavaScript function that returns a true or false
value.


All of them are correct in that they will result in the call of
checkData. However, only a) and b) will actually affect whether the form
is submitted.

The content of an intrinsic event attribute becomes the body of an
anonymous function:

/* Note that IE doesn't pass an event argument. It uses a global
* variable of the same name. Either way, you can still use:
*
* on<event>="myFunction(event)"
*
* to access the event object.
*/
formElement.onsubmit = function(event) {
/* Value in onsubmit attribute */
};

Obviously, if you don't include a return statement, no value will
actually be returned when this anonymous function exits.

With regards to the semicolon, the same rules apply as normal: semicolons
are generally optional, but it's good practice to include them. If you
had two or more statements/expressions to evaluate, a semicolon would be
necessary to separate them.

The final point I like to make is that it's simpler to pass a reference
to the FORM when it is called, rather than obtain one later. That is:

<form ... onsubmit="return checkData(this);">

function checkData(form) {
/* You can now use form, rather than
* document.forms['formName']
*/
}
It seems that Internet Explorer accepts all of the above, but I'm not
taking that to mean that all are correct.


As I said, they are all syntactically correct, but the second two are
probably not what you want.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Oli | last post by:
Hi Folks, Alright, I have a textbox called "A" and the value in that textbox is "B". When the user presses submit I want A : B to appear on the page. How do I do it? I appreciate that this...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
27
by: Terry Olson | last post by:
I'm trying to build a table on an output page based on text input by the user. And what I am trying to do is create 4 table data boxes on a row, then start a new row on the 5th one. But I can't...
10
by: iam247 | last post by:
Hi In my prototype asp page (with no javascript and no password validation, I have a registration form with the following action: <form name="form" method="post" action="RegDetails.asp"> ...
6
by: bonehead | last post by:
Greetings, I'm working on an e-mail form (btw many thanks to Philip Ronan for the very cool email address format tester function, best I've seen so far). I've been trying, with limited...
5
by: John | last post by:
Hi, I have an asp.net form where if the user enters an incorect date I change the color of the textbox to red and also display a label field with a message saying the date is invalid. What I...
7
by: Bruno Alexandre | last post by:
Hi guys, Sorry about the off topic, but I can't find the ASP group just the ASP dot NET If I want to block a user to change a form after submiting, I add a function that will disable the...
1
by: Bill_W_Stephens | last post by:
I need a form with two buttons and ability to detect which button was pressed. I got this code to work using a javascript global variable, but I know there must be a better way using DOM. Any...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.