Email Validation Method in Android Application

Any business applications in Android which has single user or multiple user access facility, it normally requires “registration” and “login” form within Application. Creating “registration” and “login” form is very simple, but validating the email address entered by the user is most important part of it.

The Android developer must check if the email address entered by the user is valid or not, as the email address is stored on server and we may need the email address to send some information like password or any other important information via email to an application user. So it is very important to validate email ID.

The user may enter some invalid email and if Android application developer hasn’t put any validations for the email, it may generate errors or unpredictable results in application as well as on server side.

To overcome this issue, Android developer needs to make some methods through which he/she can verify whether the email address entered in the form is valid or not.

Below are 2 functions created for validating the email address in any android application. 1st function “validateEmail” is for checking the validity of the email address entered by the app user. 2nd function “validateForNull” is for checking whether the email is entered by application user or not. Both function return boolen value (true/false).

The above function checks if the text in the EditText is a valid email address or not. Before validating the email address, it calls the 2nd function “validateforNull” method to check whether user has entered any text in the EditText or not. If User has entered some text then it checks the text entered by user is a valid email address or not.

The above function uses native Java classes Matcher and Pattern to check if the entered email address is valid or not. It takes the EditText to be checked for null value using the 2nd function below and if the email address is valid or not.

2) validateForNull(EditText p_editText, String p_nullMsg)

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

/**

* Method to check if some text is written in the Edittext or not

* @param p_editText The EditText which is to be checked for null string

* @param p_nullMsg The message that is to be displayed to the user if the text in the EditText is null

* @return true if the text in the EditText is not null, false otherwise

*/

privatebooleanvalidateForNull(EditText p_editText,Stringp_nullMsg)

{

booleanm_isValid=false;

try

{

if(p_editText!=null&&p_nullMsg!=null)

{

if(TextUtils.isEmpty(p_editText.getText().toString().trim()))

{

p_editText.setError(p_nullMsg);

m_isValid=false;

}

else

{

m_isValid=true;

}

}

}

catch(Throwable p_e)

{

p_e.printStackTrace();// Error handling if application crashes

}

returnm_isValid;

}

This above function uses the android’s native TextUtils class for validating if the entered text is null or not.

The above function checks if the text in the EditText control is null or not. It takes the EditText to be checked for null value and the String message that is to be displayed to the user as method parameters.

So, if you have these methods in your class, then you have to just use one condition to check if the entered email address is valid or not.

Below is the code snippet for the clicklistener for the Submit button.

I checked your code and found that it will not get compiled. Also, this toast will never be displayed to the app user because any validation would take place only on a Button click (if you put this validation code on button click event listener) or you validate the input in the Email field on TextChangedListener set for this email Input field.

No Worries below is the issue explained along with the correct way for doing this validation.

/*
* CORRECT CODE :
*
* REASON : The correct code for doing this validation would be as shown below, you need to get the Input Text into a String & then you can use the “matches” method for validating the email address with Regular expression.
*/