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

Form validation with onclick (vs. onsubmit) in WinXP's IE

P: n/a
I have created web pages that do client-side form validation using the
onclick directive. E.g.,

<form action=other_page.cgi method=post>
Enter your age: <input name=age>
<input type="submit" value="" onclick="return validate_form()">
</form>

where validate_form() returns true or false depending on whether the
user entered a valid age.

This does what I want almost everywhere (e.g., Firefox on FreeBSD,
Safari on Mac, IE on Win2000) but recently I have become aware of an
exception: IE on WinXP.

On WinXP's IE, validate_form() is called (and will display an alert box
if that's that's what I coded), but whether it returns true or false,
the form is submitted. (Then we do the server-side validation and throw
up an error page, but I'd rather catch this client-side.)

So the question I have is: why is WinXP so weird about this? Previous
versions of IE didn't do this.

While looking into this, I've discovered I can also do an onsubmit check
in the form tag, e.g.,

<form action=other_page.cgi method=post onsubmit="return
validate_form()">

Is onsubmit new or has it been around for a while? Can I trust old
Mozillas on Linux to do the right thing with it? Windows is actually a
small portion of the target audience, but I like to make things work for
everyone.

Does it make a difference if the form is submitted to the same page or a
different page? I don't have ready access to Windows XP to test this
out.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
n3*************@jetable.org wrote:
I have created web pages that do client-side form validation using the
onclick directive. E.g., <form action=other_page.cgi method=post>
Enter your age: <input name=age>
<input type="submit" value="" onclick="return validate_form()">
</form>
That form could be submitted without the user clicking on the submit button.
You're better off using onsubmit.
Is onsubmit new or has it been around for a while?


From the 1999 HTML specification:
http://w3.org/TR/html4/interact/scripts.html#h-18.2.3

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #2

P: n/a
n3*************@jetable.org wrote:
<snip>
<form action=other_page.cgi method=post>
Enter your age: <input name=age>
<input type="submit" value="" onclick="return validate_form()">
</form>

where validate_form() returns true or false depending on
whether the user entered a valid age.

This does what I want almost everywhere (e.g., Firefox on
FreeBSD, Safari on Mac, IE on Win2000) but recently I have
become aware of an exception: IE on WinXP. <snip> While looking into this, I've discovered I can also do
an onsubmit check in the form tag, e.g.,

<form action=other_page.cgi method=post onsubmit="return
validate_form()">

Is onsubmit new or has it been around for a while?
onsubmit has been around for longer than support for onclick handlers on
submit buttons.
Can I trust old
Mozillas on Linux to do the right thing with it?
Depending on what you mean by "the right thing", probably. In general,
when the _user_ submits a form (by any available mechanism for the task,
but not necessarily when a form is submitted by a script) a javascript
capable user agent will call the form's onsubmit handler(s) and allow
them to cancel the submission when necessary. The onsubmit handler is
the _only_ recommendable trigger for form validation.
Windows is actually a small portion of the target
audience, but I like to make things work for everyone.

<snip>

Form validation is probably the one area of browser scripting where all
user agents (at least those that understand what a form is and allow
them to be scripted) can be accommodated with a signal (non
feature-branching) script.

Richard.
Jul 23 '05 #3

P: n/a
David Dorward <do*****@yahoo.com> wrote:
n3*************@jetable.org wrote:
I have created web pages that do client-side form validation using the
onclick directive. E.g.,

<form action=other_page.cgi method=post>
Enter your age: <input name=age>
<input type="submit" value="" onclick="return validate_form()">
</form>


That form could be submitted without the user clicking on the submit button.
You're better off using onsubmit.


This is true. The browsers I had been working with fired off the
onclick handler even if the submit button wasn't actually clicked,
though. That may be technically wrong, but that's what they did.

I'm still curious about the behavior of WinXP's IE. I click on the
button, and the onclick handler fires off, and returns false, but the
form is submitted anyway. Isn't that wrong? Is WinXP's IE following
some specification that all those other browsers aren't aware of, or is
Microsoft just wrong here?
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.