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](https://developer.android.com/static/quick-guides/content/email_validation.png?authuser=8)
![An invalid text input with errors](https://developer.android.com/static/quick-guides/content/invalid_email_format_example.png?authuser=8)
Parent collections
![](https://developer.android.com/static/images/quick-guides/collection-illustration.png?authuser=8)
Display text
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.
![](https://developer.android.com/static/images/quick-guides/collection-illustration.png?authuser=8)
Request user input
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.