Android, Programming

Method for Email validation in Android application

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).

1) validateEmail(EditText p_editText, String p_nullMsg, String p_invalidMsg)

/**
* Method to validate the EditText for valid email address
* @param p_editText The EditText which is to be checked for valid email
* @param p_nullMsg The message that is to be displayed to the user if the text in the EditText is null
* @param p_invalidMsg The message that is to be displayed to the user if the entered email is invalid
* @return true if the entered email is valid, false otherwise
*/
private boolean validateEmail(EditText p_editText, String p_nullMsg, String p_invalidMsg)
{
    boolean m_isValid = false;
    try
    {
        if (p_editText != null)
        {
            if(validateForNull(p_editText,p_nullMsg))
            {
                Pattern m_pattern = Pattern.compile(“([w-]([.w])+[w]+@([w-]+.)+[A-Za-z]{2,4})”);
                Matcher m_matcher = m_pattern.matcher(p_editText.getText().toString().trim());
                if (!m_matcher.matches() && p_editText.getText().toString().trim().length() > 0)
                {
                    m_isValid = false;
                    p_editText.setError(p_invalidMsg);
                }
                else
                {
                    m_isValid = true;
                }
            }
            else
            {
                m_isValid = false;
            }
        }
        else
        {
            m_isValid = false;
        }
    }
    catch(Throwable p_e)
    {
        p_e.printStackTrace(); // Error handling if application crashes
    }
    return m_isValid;
}

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.

 

2) validateForNull(EditText p_editText, String p_nullMsg)

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
*/
private boolean validateForNull(EditText p_editText, String p_nullMsg)
{
    boolean m_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
    }
    return m_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.

if(validateEmail(m_etEmailAdd, getString(R.string.lbl_null_value), getString(R.string.lbl_invalid_value)))
{
// TO do: operation to be performed if entered email address is valid
}

Below are the screen shots that describe example cases when user enters invalid or blank email address and submits the data.