473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XHTML Strict and Javascript Form Validation

I have a very basic form validation script, which wont work due to XHTML
Strict not allowing me to use the name attribute on a form. Here is part of
my code:

if (document.feedback.first_name.value == "") {
alert ('Please enter your first name.');
document.feedback.first_name.focus()
return false;
}

So I figured I'd use the id attribute instead of the name attribute, like
so:

if (document.getElementById("feedback").first_name.va lue == "") {
alert ('Please enter your first name.');
document.getElementById("feedback").first_name.foc us()
return false;
}

which works in Mozilla, but not Internet Explorer. Internet Explorer just
ignores it and submits an invalid form. What am I supposed to do?

--
-Robert Smith
-------------------------------------------------------------------------------------------------------------
Remove 'nospam.' from my email address if you wish to reply via email.
Jul 23 '05 #1
3 3374
Robert Smith wrote:
I have a very basic form validation script, which wont work due to XHTML
Strict not allowing me to use the name attribute on a form. Here is part of
my code:

[...]

The following works in both Firefox and IE. I tried changing the id
attribute of the input to a name attribute and it still worked fine.
So I guess this is one of those "can't replicate the problem" problems.

The obvious one is that attribute names must be lower case, but I'm
sure you'd have that covered. Here is the W3C description of the
differences between HTML and XHTML:

<URL:http://www.w3.org/TR/xhtml1/#diffs>

I assume I got the doctype correct? You didn't say exactly which one
you are using.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>XHTML test</title>
</head><body>
<form action="" id="feedback">
<input type="text" id="first_name" value="rob">first name<br>
<input type="button" value="click test" onclick="
if(document.getElementById('feedback')) {
alert(document.getElementById('feedback').first_na me.value);
} else {
alert('eEBI doesn\'t work');
}
">
</form>
</body>
</html>

--
Rob
Jul 23 '05 #2
On Tue, 30 Nov 2004 06:45:25 GMT, RobG <rg***@iinet.net.auau> wrote:
Robert Smith wrote:
I have a very basic form validation script, which wont work due to
XHTML Strict not allowing me to use the name attribute on a form. Here
is part of my code: [...]

The following works in both Firefox and IE. I tried changing the
id attribute of the input to a name attribute and it still worked
fine. So I guess this is one of those "can't replicate the
problem" problems.


I think you misunderstand. The name attribute is perfectly acceptable on a
form *control*, and if you intend to submit the data that control
contains, it *must* be specified just as must with HTML. The name
attribute on the form element is the problem as XHTML Strict doesn't allow
it.

In fact, name attributes are only allowed on images, forms and links for
backwards compatibility with old browsers like NN4 which were written
before the introduction of the id attribute. Unless you care about them,
the name attribute should *only* be used for form controls.

[snip]
<form action="" id="feedback">
[snip]
if(document.getElementById('feedback')) {


You don't need to use getElementById. The forms collection can be
subscripted with numbers, names, or ids. With a string subscript[1], the
user agent will attempt to find a matching id first. Only if that fails
will a name look-up be performed.

document.forms['feedback'].elements['first_name']

[snip]

Mike
[1] Every expression inside square brackets is treated as a string,
including numeric literals and object references. However, if the string
can be converted to a number, then back to a string, and still match the
original value exactly, it is considered to be an array index, not a
property. So:

'10' -> 10 -> '10' '10' array index
'05' -> 5 -> '5' '05' property name
'fg' -> NaN -> 'NaN' 'fg' property name

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsh9yzed6x13kvk@atlantis...
On Tue, 30 Nov 2004 06:45:25 GMT, RobG <rg***@iinet.net.auau> wrote:
Robert Smith wrote:
I have a very basic form validation script, which wont work due to
XHTML Strict not allowing me to use the name attribute on a form. Here
is part of my code:

[...]

The following works in both Firefox and IE. I tried changing the
id attribute of the input to a name attribute and it still worked
fine. So I guess this is one of those "can't replicate the
problem" problems.


I think you misunderstand. The name attribute is perfectly acceptable on a
form *control*, and if you intend to submit the data that control
contains, it *must* be specified just as must with HTML. The name
attribute on the form element is the problem as XHTML Strict doesn't allow
it.

In fact, name attributes are only allowed on images, forms and links for
backwards compatibility with old browsers like NN4 which were written
before the introduction of the id attribute. Unless you care about them,
the name attribute should *only* be used for form controls.

[snip]
<form action="" id="feedback">


[snip]
if(document.getElementById('feedback')) {


You don't need to use getElementById. The forms collection can be
subscripted with numbers, names, or ids. With a string subscript[1], the
user agent will attempt to find a matching id first. Only if that fails
will a name look-up be performed.

document.forms['feedback'].elements['first_name']

[snip]

Mike


Thankyou, the document.forms[] way worked. I actually used

document.forms['feedback'].first_name.focus()
return false;

but I can't figure out why

document.getElementById('feedback').first_name.foc us()
return false;

doesn't work for me. Thanks for solving my problem though :)
Jul 23 '05 #4

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

Similar topics

1
2106
by: Gordon - Adelphia | last post by:
I have a question regarding xhtml. Why, why, why does the ELEMENT <body> allow “unblocked” text. HTML does not (though, most browsers will render). Xhtml (transitional) however allows text nodes...
6
2155
by: Jonny | last post by:
How can you validate Javascript generated HTML for XHTML 1.0 strict compliance? To avoid the "<" and "&" problem, all inline scripts MUST be enclosed with either: <!-- script --> Looked down...
41
1248
by: CMAR | last post by:
What are the pluses and minuses of constructing and validating between XHTML Transitional vs. HTLM 4.01 Strict Thanks, CMA
9
2327
by: rbronson1976 | last post by:
Hello all, I have a very strange situation -- I have a page that validates (using http://validator.w3.org/) as "XHTML 1.0 Strict" just fine. This page uses this DOCTYPE: <!DOCTYPE html PUBLIC...
22
2834
by: Gianni Rondinini | last post by:
hi all. please excuse the misusage of some tech terms, but writing in english is not as easy as in italian :) i'm designing our new website and, since i want to do something that will last as...
9
3123
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
11
3079
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
8
2103
by: gordon.is.a.moron | last post by:
Hello, I was pondering creating form filling software. As a sample I decided to take a look at the sign up page for hotmail (http://get.live.com/mail/overview then Signup) Probably not the...
10
2043
by: Robert Huff | last post by:
Can someone offer suggestions why, on the same server (Apache 2.2.8), this works <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en-US"> <head> <link rel=stylesheet...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.