In this tutorial you explore two HTML form fields that allow visitors to enter text: text fields, suitable for short, single-line text, and textarea fields, which are suited to longer paragraphs of text.
Possibly the most widely used form field is the text field. This is a simple box into which the user can enter small amounts of text data, such as their name or email address. It takes the format:
<input type="text" name="xxxx" value="xxxx" size="xxxx" maxlength="xxxx">
name attribute is the name of the text field (for example,
value attribute allows you to provide a default value that will appear in the box (the user can change the value if they want). This is optional – to leave the field blank, use
value="" or miss out the
value attribute altogether.
size attribute specifies the physical size (width) of the text field, in characters. You can miss out this value in which case the browser’s default field size will be used.
maxlength attribute allows you to limit the amount of characters the user can enter. If you leave this value out, then the user can enter as much as they like.
Text field example:
Email Address: <input type="text" name="email_address" value="" size="30" maxlength="50">
If you need the user to input a large amount of text (more than a single line), use a
textarea field. It takes the format:
<textarea name="xxxx" rows="xxxx" cols="xxxx" wrap="xxxx"> (default text here) </textarea>
name attribute is the name of the field (for example,
rows attribute specifies the height of the textarea in characters, while the
cols attribute specifies the width.
wrap attribute controls how the text is wrapped when it reaches the right-hand edge of the textarea box. Possible values are:
- The text will not wrap, and will be submitted exactly as it was entered by the user.
- The text as displayed in the textarea box will wrap, but it will be submitted exactly as it was entered by the user (i.e. with no wrapping).
- The text in the textarea box will wrap, and it will be submitted with line breaks inserted where the text wraps (i.e. exactly as it appears in the box).
<textarea></textarea> element, you can place some text that will be displayed in the textarea when the form loads. The user can then change or delete this text as appropriate.
Textarea field example:
Please enter your comments below:<br> <textarea name="comments" rows="5" cols="50" wrap="soft"> I love this site! </textarea>
Other types of form fields
Now that you understand text and textarea fields, find out about: