tags:

views:

68

answers:

4

Why this give error in W3c html validation ? I'm using HTML 4.01 Strict doctype.

<form method="get" action="/search" id="search">

   <input type="text" value="search" maxlength="80" class="textbox" >

</form>

and this does not?

  <form method="get" action="/search" id="search">
    <div>
       <input type="text" value="search" maxlength="80" class="textbox" >
    </div>
  </form>

This is error

document type does not allow element "INPUT" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "PRE", "DIV", "ADDRESS" start-tag

Is it necessary to put input in a div?

A: 

What schema are you validating against? (sorry this isn't an actual answer, i'm not yet able to comment!)

Jim McKerchar
Schema means?? HTML is selected as a tag and this is the first line of question,I've clearly wrote in first line "Why this give error in W3c html validation ? What is not clear?
metal-gear-solid
there are different versions and variants of html jitendra
meandmycode
... what meanmycode and others said ;) just wondered whether you were using HTML, Xhtml, strict/transitional etc...
Jim McKerchar
+3  A: 
<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

A form can contain a block element (with the exception of another form) or a script element.

An input is not a block element, but most block elements may contain inline elements, which include inputs.

David Dorward
+3  A: 

With strict validation you need a block element around your input fields; Best choice here would be to go with a <fieldset>.

D4V360
ok is it necessary to use legend and lable along with fieldset?
metal-gear-solid
I think it's not necessary but it's good for the semantics;You can just insert them and hide them with CSS if you don't want to display them.
D4V360
no i checked it's necessary i added "fieldset" but now validator is asking for "legend". fieldset along with legend is must for validation but we can use blank legend tag no need to put anything in <legend>...</legend>. So i think instead of using Fieldset and blank legend <div> would be good option. what you think?
metal-gear-solid
In most cases the LEGEND element is useful for the document, it tells what this group of inputs is about. If you really don't need it, use a simple P element instead.
naivists
but what if form has only 1 element, and is <p> more semantic <div> ? and what about <span>
metal-gear-solid
span isn't a block level element by default so that doesn't make sense. Fieldset is the tag that fits most here. If you don't need your legend hide it with CSS (display: none;) and insert a description of what kind of form it is in the legend. If you don't want to use the fieldset just use a div or P element; They validate but both are less semantic I guess.
D4V360
@D4V360 - ok. but if we can use blank <legend></legend> which is w3c valid i tested then should i use as it is or i t would be good to put something in <legend>some text </legend> then hide from CSS. Which method would we good for accessibility and cross browser compatibiliy? <div> , <p>, <fieldset><legend><legend> or <fieldset><legend>some text<legend> with display:none for legend.
metal-gear-solid
For accessibility go with the filled in legend, since it describes what the form is all about to for example Google or a speech browser. For browser compatibility it doesn't matter, display: none is widely supported and part of all of todays HTML standards.
D4V360
+1  A: 

The official reason is that in HTML4 strict the FORM elements can only contain block elements, but block elements are in turn allowed to contain form inputs. Hence, the structure form->block element->form input is what has to be used.

I cannot find a serious reason, WHY it is so. It sounds like the authors of HTML did not want to allow forms that start on one line, then wrap over to next line and then have a submit button somewhere on the third line (which you can still achieve, by redefining the display property of a form, if you need. For instance, this article on 24ways just states that "it is a difference between Transitional and Strict standards".

Another comparison states (they are talking about xhtml vs html, but the idea is the same):

In XHTML, the elements need to be coded in a semantic manner. Tables and forms can not be included in paragraphs, but form elements, being inline elements, need to be contained within a semantic block level element, such as a paragraph or table cell.

This makes me think that FORM element is not "semantic enough" to contain other elements. It is not normally meant to be used for marking up your code, it is more like a technical element which shows where to post the data to. Hence, it technically is a BLOCK element, but it needs something more "semantic" to contain the actual input fields.

naivists