• Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • #Deals
  • #News
  • #WiKi
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • 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.

 

  • 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)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 1261 Views
Tags | Android
AUTHOR
Ranjan

I m Ranjan and Sharing my years of experience in Software Development. Love to code in Mobile apps (IOS, Android, Power Apps, Xamarin, Flutter), Machine Learning ( Beginner ), Dot Net, Databases ( SQL Server, MySql, SQLite), WordPress, Cloud Computing ( AWS, Azure, Google, MongoDB) and many more as required on project-specific. Besides this love to travel and cook.

You Might Also Like

thumb-android-studio1

I’m Android Studio!

July 4, 2021
android-11-for-developers1

Hello Developers, Android 11 has some new features for you

July 31, 2021
mycodetips-newlogo2

Why my Android emulator is very slow

September 27, 2013
Next Post
Previous Post

Subscribe for updates

Join 5,733 other subscribers

whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

Latest Posts

  • 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
  • swift-concurrency-await
    Concurrency in Swift
  • time-complexity-dsa
    The Term Time Complexity in DSA
  • objective-c-datatypes1
    Objective-C Data Types
  • Convert-jpeg-word
    Convert JPG to Word – Tips You Should Try!
  • objective-c-control-statements2
    Objective-C control statements and loops !

Quick Links

  • #about
  • #myapps
  • #contact
  • #privacy

Other Websites

  • #myQuestions
  • #myBhojanalaya
  • #gadgetFacts
  • #ifscCodesDB

Tag Cloud

Android Android Studio API APP Programming Apps ARC asp.net blogging Browser Config CSS DATABASE DFD error Features GUI HTML HTML5 IDE IIS installation Interview Questions IOS iPhone javascript Mac objective-c OneDrive OS Programming quicktips SDK SEO Settings SMO SQL swift swiftUI Teams Tips & Tricks Tools UI Web Wordpress Xcode

©mycodetips.com