280

Is it valid to have <input> without it being in a <form>?

Have a process for marking up some fields in pages and just found a page with input fields that where not being marked up as I expected.
It's taken me a while but worked out that the process of getting the form elements then getting the fields is what caused these to be missed because there is no form.

aksu
  • 5,221
  • 5
  • 24
  • 39
Greg Domjan
  • 13,943
  • 6
  • 43
  • 59
  • @Adriano - my thought exactly. just run it through and see! – Gabriel Hurley Jul 20 '10 at 21:42
  • 1
    Sure [w3c validator](http://validator.w3.org/), I really should have added that an explanation behind why would be nice. – Greg Domjan Jul 20 '10 at 22:34
  • I mean also that it might be valid for some versions of html, but not for others, and in that case, the validator is much faster than perusing the standards. – Adriano Varoli Piazza Jul 21 '10 at 13:01
  • 3
    See also: [Is there any danger to using input fields outside/without forms in HTML/Javascript pages?](http://stackoverflow.com/q/8568660/1591669) – unor Sep 09 '13 at 16:51

7 Answers7

238

<input> without a <form> appears valid, yes (at least for html 4.01, look near the end of 17.2.1):

The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on intrinsic events. Note that controls outside a form cannot be successful controls.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • I have just created an `` tag outside of a form in Visual Studio 2010 and it had no problem with it. So I agree it is odd, even pointless, but not illegal. – Philip Smith Jul 20 '10 at 21:47
  • Ta, was having trouble finding that info – Greg Domjan Jul 20 '10 at 22:26
  • 1
    then what is the point to use form and input name anymore? we can all use input with id, isn't that easier for everyone? – Ivan Wang Jun 11 '12 at 13:36
  • 8
    To be able to submit form data to a server (Ajax aside). – Ludovic Kuty Sep 09 '13 at 08:07
  • 5
    @PhilipSmith I know your comment was written a *long* time ago. But why do you think this is odd or pointless? Why would I want to create a form if the control is not communicating with a server? – adam-beck Mar 25 '15 at 18:00
  • 23
    What's a "successful control"? – AlikElzin-kilaka Jul 11 '15 at 12:14
  • 31
    _When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form. Those controls for which name/value pairs are submitted are called successful controls._ [w3c](http://www.w3.org/TR/html401/interact/forms.html#form-controls) – SantiBailors Nov 12 '15 at 10:08
  • 5
    @adam-beck For example, a certain AngularJS website handles inputs, manipulates data, and sends it to an api. HTML templates for forms may be written without any form tags and they communicate with the server just fine. – sr9yar Aug 01 '16 at 20:39
95

I checked the following with the W3C validator and it confirms this is valid.

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
</head>
<body>
  <input type='text' />
</body>
</html>
user664833
  • 18,397
  • 19
  • 91
  • 140
Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47
19

Reference to a more up-to-date specification:

HTML 5.2 - W3C Recommendation (14 December 2017)

A form-associated element can have a relationship with a <form> element, which is called the element’s form owner. If a form-associated element is not associated with a <form> element, its form owner is said to be null.

David
  • 6,695
  • 3
  • 29
  • 46
14

According to MDN it is possible:

Note that it's always possible to use a form widget outside of a element but if you do so, that form widget has nothing to do with any form. Such widgets can be used outside a form, but then you should have a special plan for such widgets, since they will do nothing on their own. You will have to customize their behavior with JavaScript.

HTML5 introduces the form attribute on HTML form elements. It should let you explicitly bind an element with a form even if it is not enclosed within a form tag. Unfortunately, for the time being, the implementation of this feature across browsers is not yet good enough to rely on it.

Community
  • 1
  • 1
Adnan Faradhi
  • 161
  • 1
  • 6
13

I know this question is quite old, however, I have successfully built many complex data entry pages without form tags. Although it isn't considered "standard" by many, it is NOT inappropriate to use inputs without a <form>. My projects were instances where I needed complete control over how the page behaved and the default form behavior was getting in the way. Was able to perform page and field level validation ( using JS ) and "submitted" the data with Ajax calls etc...in fact, this is my preferred way these days.

Lots of JS is required, but its not that difficult and is easily done as reusable code.

There are also other instances where I def do NOT use forms with inputs such as Login Pages.

Hope this testimonial helps someone.

G-Man
  • 1,138
  • 16
  • 20
4

in my point of view , we can use input outside the form and even send data to the server without put the input in the form ,but for SEO and website readability(for visually impaired people)(just the same reason for the some semantic content tags in HTML5 , like section ,footer, header something like that ), we have to use input in the form tag.It is important that we ensure the code we use is available to all people including the visually impaired people because it is not just about websites, it is about providing access to information for everyone.

2

Yes, you can have a valid input without a form.

heisenberg
  • 9,665
  • 1
  • 30
  • 38
  • 23
    Well, you could have added some evidence behind the 'yes'. Or a clarification of which versions of HTML this is true for. Still, I wouldn't have downmodded you. – Adriano Varoli Piazza Jul 21 '10 at 12:59
  • 10
    So with your rationale why even have SO at all if the user can just go to W3 and look it up himself? I wouldn't have downvoted you either, but I'd have to agree that the one with more explanation in a clear and concise manner gets the upvote, because an explanation as well as the correct answer is what is most helpful here at SO. – Mattygabe Jan 19 '11 at 16:43