Validate input as the user types

You can validate input as the user types in a text field, such as entering a name, email, address, or other contact information. This validation reduces errors and saves your users time.

Version compatibility

This implementation requires that your project minSDK be set to API level 21 or higher.

Dependencies

Validate input as the user types

Use the following code to display the field input and validate the text while the user types. If the information is not validated, an error message helps the user correct the input.

Key points about the code

  • Defines a composable that reuses the OutlinedTextField component, adding the required parameters to display validator error messages as user types.
  • EmailViewModel is used to maintain state and provide the email validation logic.
  • if isError is true, the UI provides a visual indicator of a validation error state.
  • The component will display "Incorrect email format." until a complete, correct email is input.

Results

A valid text input
Figure 1. A text input field with email validators displaying no error messages for a valid email address.
An invalid text input with errors
Figure 2. A text input field displaying an error message when an invalid email address is entered.

Parent collections

Text is a central piece of any UI. Find out different ways you can present text in your app to provide a delightful user experience.
Learn how to implement ways for users to interact with your app by entering text and using other means of input.

Have questions or feedback

Go to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts.