0

I'm trying to write code that validates email address, and I came across the following source code that allows for proper validation of email address, however when I tried implementing the code on android studio, it did not recognize the following coding items; &amp, &gt and !m_matcher

Source Code:

/**
 * 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;
}

Part 2:

/**
* 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;
}

Could someone please explain to me what &amp and &gt is and why android studio does not recognize these items. And finally, why is the exclamation point in this line of code underlined in red **!**m_matcher

Apologies for the long post and thanks in advance!

NoobCoder
  • 11
  • 6
  • Where did you copy this code from? – OneCricketeer Nov 16 '16 at 18:41
  • @cricket_007 Got it from here : http://www.technetexperts.com/mobile/method-for-email-validation-in-android-application/2/ – NoobCoder Nov 16 '16 at 19:01
  • It partially works. It sets the error if the field for entering email address is null, however, it does not check the format. This is quite a conundrum. – NoobCoder Nov 16 '16 at 19:02
  • This stackoverflow question has some good answers for email validation: http://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method – dnault Nov 16 '16 at 19:09

1 Answers1

4

You need to use && for & & and > instead of > - looks like you've copied from a web page which has HTML encoded the code.

Change this line from:

if (!m_matcher.matches() && p_editText.getText().toString().trim().length() > 0)

to:

if (!m_matcher.matches() && p_editText.getText().toString().trim().length() > 0)
ddavison
  • 28,221
  • 15
  • 85
  • 110
Squiggs.
  • 4,299
  • 6
  • 49
  • 89