Free HTML course. Sign Up for tracking progress →

HTML: Text input field

The main element in HTML forms is the input box. It's implemented using the <input> tag and allows you to specify various fields for data entry: text, passwords, checkboxes, radio buttons, submit buttons, file upload, date input, and so on.

Since the HTML5 standard, the list of fields has been expanded with many new options. The basic types of inputs are:

  • Text
  • Password
  • Checkbox
  • Radio button

The easiest way to use <input> is to create a text input box. <input> is present in all search fields. To create a simple text field to enter data, you must add a tag <input> inside the form and specify its type attribute with a value of text:

<form>
  <input type="text">
</form>

label tag

The field has been created, but it doesn't say anything about what should be entered there: first name? or last name? the code to a safe? To specify the purpose of the field, you should use the <label> tag. This is a paired tag that resembles a paragraph but refers specifically to the description of a form field.

<form>
  <label>Enter a name</label>
  <input type="text">
</form>

Actually, it's not enough just to specify a label, you need to link it with a form field. You need this to make sure that information is communicated unambiguously since there can be lots of fields.

You can use one of two options to show the label:

  • Linking via attribute id. To do this, the <input> tag is specified with the id attribute with an arbitrary value, and the for attribute is added to the <label> tag with the same name as the value of the input's id
<form>
  <label for="name">Enter a name</label>
  <input type="text" id="name">
</form>
  • Linking through nesting. Did you notice that <label> is a paired tag? You can insert an input inside the label, and then they will automatically link to each other
<form>
  <label>Enter a name
    <input type="text">
  </label>
</form>

placeholder attribute

There are situations where there is a description for the field, but it's unclear what data should be put in there. For example, should it be first name and then last name, or vice versa? The <input> tag's placeholder attribute can be used to help the user. The value of this attribute will be displayed inside the text field.

<form>
  <label for="name">Enter name and surname</label>
  <input type="text" id="name" placeholder="John Smith">
</form>

Other text inputs

Have you noticed that when you enter passwords, the browser automatically puts asterisks instead of displaying characters? This can be achieved by specifying a password value for the type attribute:

<form>
  <label for="pin">Enter a pin code</label>
  <input type="password" id="pin" placeholder="1234">
</form>

The HTML5 standard has some more interesting types for the <input> tag that implement the usual text box but do it in their own way. For example:

  • email
  • number
  • search
  • tel
  • url

Their purpose is to ensure that the values are automatically checked for correctness by the browser. For example, if you assign an email value, the browser will expect the correct format for an email address. Otherwise, it will indicate that the value is invalid when you try to submit the form.

Mobile devices can't bypass these fields either. In modern versions of mobile operating systems, the keyboard adjusts to the type of field being filled in. If the email type is set, the @ symbol will be added to the keyboard immediately, so you don't have to search for it amongst the additional characters

Instructions

Create a form with a data handler file at /people. Create two text fields inside the form.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community