How to Easily Validate User Inputs on Android?

İsmail Emre Bayırlı
Huawei Developers
Published in
5 min readNov 4, 2021

--

Introduction

In this article, I will talk about how user inputs can be easily validated in different situations. So what is validation user input? First, let’s talk about this.

Let’s say you have a login screen in your app. You get the user’s username, email, and password to log in.

These values can not be empty and you can define some extra input formats for these values also. E.g; Password input cannot be less than 6 and greater than 12 or the user must enter a suitable email address.

If the user does not comply with these input format rules we need to show warning messages to the user. If we do this job in the old way we need a lot of if-else condition. E.g; first we need to control input is empty or not then we need to control the input comply input formats such as password format, etc. Also, we should show different warning messages to the user for each condition. One downside of the old method is that we have to do the same things again if we want to check user input on other screens.

Thanks to the structure that I will explain in this article, we create user input formats, we will be able to use them in different scenarios. We will get rid of a lot of if-else conditions.

In this article, I will use Kotlin for development and I will use ViewBinding to communicate with UI elements.

Let’s start then. Let’s say you have a login screen as above. (You can get the source code below.)

You have three input formats;

1-) You want to control the inputs are empty or not.

2 -) You want to control the email address is suitable or not.

3-) You want to control the password by more than 6 and less than 12.

And you want to show different warning messages to the user for each condition and input area.

First, we will create a package named validator and then we will add another package named base to this package as below.

Then we will need a data class. We will create ValidateResult data class as below.

Using this class we will obtain validation result and warning messages. Then we will create the IValidator interface as follows.

We will implement this interface in our validator classes that we will create later. And we want to create such inheritance relation between validator classes, in this way, we will use these validators together as varags.

Then we will create the BaseValidator abstract class as follows.

The validator classes that we will create later will extend this class. We will also use the fun validate(vararg validators: Validator) method to check for multiple formats.

So far we created the necessary structure. Now, we will create validator classes for different formats. In this article, we will control if the input is empty, the e-mail address is suitable, and the password length is between 6 and 12. Therefore we will create three validator classes.

EmptyValidator

In the validator package, create a class named EmptyValidator as below;

This class will take a string value as user input and extends BaseValidator class. In validate() method we control the input is empty or not then we return ValidateResult value according to the validation result.

EmailValidator

In the validator package, create a class named EmailValidator as below;

In this class, we control the input is empty or not then we control email address is suitable.

PasswordValidator

In the validator package, create a class named PasswordValidator as below;

In this class, we control the input is empty or not then we control the input password is complying with our format.

After validators creation your package structure should look like this;

You can add more validator classes as above samples, using this package structure will make your code more organized.

Examples of validators usages

As I mentioned above, we can use these validators alone or together. In the login scenario, the username has no specific input format. We just need to control username input is empty or not.

As you can see above, we use to obtain user input from input edit text then we use EmptyValidator() alone to control username input is empty or not. After according to the validation result we show a warning to the user or a clear warning message from the input layout.

Let’s talk about multiple validator usage at the same time. For email validation, we will control the input value as below;

We had created a function in the BaseValidator classes and this function was taking vararg IValidator values. We use this function to validate input values for different formats at the same time.

Thanks to this function, if the input is empty function it will return an empty input warning message or if the input is not suitable for email format, function will return a suitable warning message.

We will control password input by using multiple validator classes as above;

If you want to see these usages together;

Screenshots

Summary

In this article, we talked about validating user inputs using different structures. We can create as many different input formats as we need and use them easily on different screens.

Thank you.

--

--