NoobCoder NoobCoder - 18 days ago 5
Android Question

Explanation on what &amp means in code for validating email address

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!

Answer

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