• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Interview
  • #Tips

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
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.

 

Liked it? Take a second to support Ranjan on Patreon!
Become a patron at Patreon!
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 1575 Views
Tags | Android
AUTHOR
Ranjan

Namaste, My name is Ranjan, I am a graduate from NIT Rourkela. This website is basically about of what i learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures, its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

android-plugins

Android Studio Plugin that Makes it Most Useful

July 30, 2021
mycodetips-newlogo2

How to prevent SQL Injection in iOS apps?

October 7, 2013
mycodetips-newlogo2

Simple Sliding Menu Example in Android

February 20, 2014
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,220 other subscribers

Latest Posts

  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
  • Designing Mobile APPS
    Limitation and restriction of designing mobile apps
  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

.Net Android Blogging Cloud Concept Database DSA ERROR ExcelTips Flutter Interview IOS IOSQuestions JAVA Javascript MAC-OS No-Mouse Objective-c Programming Quick Tips SEO Software Design & Architecture Swift SwiftUI Tips&Tricks Tools Troubleshoot Videos Web Wordpress Xamarin XCode

  • What Business Problems Solves in Low Level Design (LLD)
  • SRP : Single Responsibility Principle in Swift and Objective-C
  • Limitation and restriction of designing mobile apps
  • SOLID Principles of Software Design
  • Latest features in IOS 16
MyCodeTips

©mycodetips.com