9.7. Text Input Boxes

The text input field is one of the most used fields in HTML forms. This field allows for the input of one line of text—generally used for names, addresses, search terms, and so on.

The text input field tag has the following format:

<input type="text" name="name_of_field" id="id_of_field" value="initial_value"
  size="size_of_field" maxlength="max_characters_allowed" />

Although all the attributes previously listed are not required, they represent the minimum attributes that you should always use with text input fields. The following sample text box is displayed 30 characters long, accepts a maximum of 40 characters, and has no initial value:

<p>Name: <input type="text" name="username" id="username" value=""
  size="30" maxlength="40" /></p>

The following code example defines a text box that is displayed as a box 40 characters long, only accepts 40 characters, and has an initial value of "email@example.com" (supplied via the value attribute):

<p>Email: <input type="text" name="email" id="email"
value="email@example.com" size="40" maxlength="40" /></p>

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.