Learning Material Design Lite: Text Fields

Next up in our series learning the ins and outs of Material Design Lite (MDL) we’re going to look into the Text Fields component. A text field could be used on anything from a search form, a comment form or a contact form, and often we see it in conjunction with the button element.

In fact, the text fields component in MDL is similar to the button component; it is an enhancement of a standard html element to bring it inline with the overall Material Design aesthetic and specification. Once again, before we begin, you will need to include the MDL libraries—the stylesheets and the JavaScript—in your document’s head.

Let’s begin with some basics.

Basic Text Field

To implement the text field component, we start off with an empty div with mdl-textfield and mdl-js-textfield classes, wrapped within a form element. If you have been following our previous posts in this series, you should already be familiar with the class naming pattern in MDL. In this case, the mdl-textfield class applies the visual enhancements from the CSS, while the mdl-js-textfield applies dynamic behavior through the JavaScript.

Within the div, we add an input element (or perhaps a textarea) and a label (which is required) along with the relevent class needed to apply the styles.

That’s all there is to it; we have just built a basic text input field with MDL text field component!

Potential Issues

It’s worth noting that the mdl-textfield__input class we added to the input element will only work well when applied to a text-based input type such as text, password, email, tel, url, and search. Applying the class to an input element of type color, date, or file might churn out a very odd result.

Additionally, the label text will appear equally strange if we also add placeholder text:

The input field with placeholder

Floating Label

We can improve the input element with Floating Label. As the user is focusing on the input field the label will float upwards, out of the way of the cursor. The floating label is an increasingly common pattern in form design, particularly in the mobile landscape where limited space is of concern.

MDL has made it really easy to apply this design pattern. Back to the previous HTML code, we need to add the mdl-textfield--floating-label class to the div container.

Bingo! Our text input field should now be more visually pleasing with the floating label:

Expandable Input Field

The Expandable Input Field is yet another common design pattern which we can implement in MDL. We first see the input as an icon. Upon being clicked, it will expand into an input field. We usually find this design pattern applied to search inputs.

To make our previous input expandable, we need to add the mdl-textfield--expandable class to the div container. We also wrap the input element and the label with a new div with the mdl-textfield__expandable-holder class attached. Following this workflow, we also change the input type, the text label, as well as the related attributes to specify “search”.

Then, we create a button outside outside the mdl-textfield__expandable-holder, using a label element pointing to the input.

Input Validation

Security and sanitizing data are crucial where forms are concerned. We do not want our users to submit entries with incorrect or even malicious content. We therefore need to evaluate the user entry in the input field to comply with the input type. If the input is of email type, the entry must comply with an email format—it should contain @ and end with a TLD (top level domain).

Fortunately, MDL helps us out-of-the-box. We don’t have to add extra markup, or classes. Set the input with a proper type, and it will turn the underline red if the entry formatting does not adhere to the input type.

An error

But we can also set our own formatting standard, and also provide a visual element that displays an error message. For example:

MDL input error message

To display an error notification, we first need to provide limits to what users can enter in the input element through the HTML pattern attribute. The pattern attribute takes a Regular Expression (Regex) that will evaluate and validate the user input.

In this case, I would like the username input to be alphabetic, without spaces. Thus, we will specify the pattern attribute with the following Regex: [A-Z,a-z]*.

Note: you can find common Regex patterns in HTML5Pattern, such as one for validating email, password, phones, postal codes, and dates.

Next, we add the error message below the label using a span element with the mdl-textfield__error. You might also need to apply style rules to adjust its position and the content alignment.

We are all set! Now, the input should throw an error message below the input, and highlight it with red, if the user inputs numbers, special characters, or spaces. Give it a go:

Wrapping Up

As we have just discovered, the MDL text fields component makes it easy for us to implement engaging input fields with helpful design patterns like the floating label and expandable input field. We also discovered that it’s easy to add error message in case of incorrect formatting.

The next tutorial should be an exciting installment of this series. We are going to look into a rising star among UI elements, the Card. Stay tuned!