Form validation – Login and Registration – Android


Form validation, probably one of the most important topic in android. Most of the apps you see on Google Play Store have a login and registration feature. If notice carefully there are many apps which have registration and login form, but the fields in those forms are not perfectly validated.

In this article, I am going to show you how to validate each and every field of a form such as a login or registration form in Android.

Our registration form will have six EditTexts, two RadioButtons, one TextView and one Button.

Overview of the validations –

  • All the fields in the registration activity are mandatory.
  • The name filed will have simple validation like the first letter of a word should be capitalized and multiline is not allowed.
  • There will be two email fields, where the user has to confirm the email. We will also use TextWatcher and call addTextChangedListener to check if the email is valid.
  • Like the email fields, there will be two EditText fields for the password.
  • There will be an EditText for the phone number. The maximum length of the phone number field will be 10.
  • Two radio buttons. The user has to select the gender, otherwise, a warning message will be shown.
  • A TextView, clicking on the TextView a DatePicker dialog will open. The user must be 13 or older.


These are the most common fields in a registration form. So let’s create the registration activity and its layout file. Name it RegistrationActivity and actvity_registration.

Below is the layout code of the RegistrationActivity.

And the file –

At first, we will create a method for our DatePicker. We will call this method when the user clicks the DOB TextView.

Create a private method and name it selectDob(). We will subtract 13 years from the current date. So the user has to 13 or older.

Final code of our selectDob() method – Minimum age should be 13

Now that our selectDob method is ready, let’s validate all the other fields.

Final code of our register button click listener

As you can see from the above code we are checking if there is an empty EditText field.

To check for empty field we are using trim().length() > 0.

What this trim method does is, it eliminates leading and trailing spaces. So here after eliminating any leading or trailing spaces, we are checking the length. If the length is greater than 0, then the String is not empty.

Then we are checking if the email is valid or not.

It checks if an email address is valid or not.

The only thing left for us to do is checking the if the email is valid or not as the user enters the email. This is not necessary, but this will surely make this registration form more professional. To do this we can use the callback methods of the TextWatcher interface.

Place the below code inside your onCreate method

That’s all. Now run the project and see if everything is working or not.

Final code of our RegistrationActivity activity

Now let’s create the login activity. Name it LoginActivity and create a layout file for it, activity_login.

It will have a Login button and two EditTexts, one for the email and the other one for password.

Let’s create the layout file first –

Final code of our activity_login.xml

Final code of our

Hope you have understood everything discussed above. If you have any doubt or suggestion, please let us know in the comment section below.


You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *