Search code examples
htmlformsvalidation

Is it valid to have form tags that start in one div and end in another?


Because of some JavaScript/CSS rules I have, I cannot just wrap these two divs in a form tag and be done with it.

So, I am wondering if the below HTML is considered valid?

<div id="tab1">
    <form>
        <input type="text" name="whatever" />
</div>

<div id="tab2">
        <input type="submit" value="Submit" />
    </form>
</div>

Solution

  • No. This is not valid HTML/XML.

    Even worse, it will be parsed as:

    <div id="tab1">
        <form>
            <input type="text" name="whatever" />
        </form> <!-- form will be closed here, because parent is being closed -->
    </div>
    
    <div id="tab2">
            <input type="submit" value="Submit" />
        </form> <!--  not sure about what happens with this one, but it will be either removed or replaced with empty <form></form> element -->
    </div>