Check if email address is valid in C++/MFC



 
Here is the C++/MFC code to check if an email address is valid. 
 
bool IsEmailAddressValid(CString emailAddress)
{
	emailAddress.Trim();
 
	if (emailAddress.IsEmpty()) return false;
 
	int idx;
	CString rightPart;
 
	idx = emailAddress.Find(_T('@'));
	if (idx == -1) return false;
 
	//is the first character @?
	if (idx == 0) return false;
 
	rightPart = emailAddress.Right(emailAddress.GetLength() - idx - 1);
 
	idx = rightPart.Find(_T('.'));
	if (idx == -1) return false;
 
	//character immediately after @ is .?
	if (idx == 0) return false;
 
	//is there only period?
	if (rightPart.GetLength() == 1) return false;
 
	///is the last character period and no characters after that?
	if (rightPart.GetLength() - 1 == idx) return false;
 
	return true;
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *