• Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • DSA
  • Concept
  • Interview
  • Tips&Tricks
  • Tutorial Videos
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 button
  • 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 - 1483 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

mycodetips-newlogo2

How to Set up Android Device Manager to lock and wipe your phone

September 27, 2013
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
Next Post
Previous Post

Support us

mycodetips mycodetips
Follow us @ LinkedIn 2850+

Subscribe for updates

Join 8,213 other subscribers

Latest Posts

  • YT-Featured-solidprinciples
    SOLID Principles of Software Design
  • IOS 16 Features
    Latest features in IOS 16
  • r-language
    How can R language be used for data analysis?
  • wordpress-coding-blog
    Guide To WordPress Coding Standards
  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

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

Android Android Studio API APP Programming Apps blogging CSS DATABASE dsa Features HTML HTML5 installation Interview Questions IOS iPhone javascript Mac objective-c OS Programming quicktips SDK SEO SQL swift Tips & Tricks Tools UI Web Wordpress Xcode

  • SOLID Principles of Software Design
  • Latest features in IOS 16
  • How can R language be used for data analysis?
  • Guide To WordPress Coding Standards
  • What is Algorithm?

©mycodetips.com