HTML Text and Textarea Form Fields

Learn how to use text fields (for single lines of text) and textarea fields (for multiple lines of text) within HTML forms.

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.

Find out how to create forms in our HTML forms tutorial.

Text fields

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">

The name attribute is the name of the text field (for example, "email_address" or "age"). The 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.

The 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.

Finally, the 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">

Email Address:

Textarea fields

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>

The name attribute is the name of the field (for example, "comments"). The rows attribute specifies the height of the textarea in characters, while the cols attribute specifies the width.

The wrap attribute controls how the text is wrapped when it reaches the right-hand edge of the textarea box. Possible values are:

off
The text will not wrap, and will be submitted exactly as it was entered by the user.
soft
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).
hard
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).

Inside the <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>

Please enter your comments below:

Other types of form fields

Now that you understand text and textarea fields, find out about:

Follow Elated

Related articles

Responses to this article

2 responses (oldest first):

20-Apr-10 13:38
am a learner, can anybody there help to write simple registration and login form?

thanks
25-Apr-10 00:00
Hey giwababatunde,

Can you be more specific? What form fields do you need in the form? Do you have a server-side script (or scripting language) in mind?

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page