
Recently I wanted to test and validate an email address so the best way to do is to use the regular expressions.
The simple way is the use the one from Windows. (in the vbscript.dll)
// import the second table from vbscript
#import <vbscript.dll> tlbid(2) no_namespace named_guids raw_interfaces_only
CComBSTR bstrEmailValidation ( L"^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,6}$" );
CComPtr<IRegExp> m_pRegExp;
bool bIsValid = false;
HRESULT m_hrCoInit = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
if ( SUCCEEDED( m_hrCoInit ) )
{
HRESULT hr = m_pRegExp.CoCreateInstance( __uuidof(RegExp) );
if ( SUCCEEDED( hr ) )
{
hr = m_pRegExp->put_IgnoreCase ( VARIANT_TRUE );
hr = m_pRegExp->put_Global ( VARIANT_TRUE );
hr = m_pRegExp->put_Pattern ( bstrEmailValidation );
VARIANT_BOOL bMatch = VARIANT_FALSE;
hr = m_pRegExp->Test( CComBSTR(strText), &bMatch );
if ( SUCCEEDED( hr ) )
{
bIsValid = (bMatch == VARIANT_FALSE) ? false : true;
}
}
}
// and later uninit
m_pRegExp = NULL;
if ( SUCCEEDED( m_hrCoInit ) )
{
CoUninitialize();
}
If you need more info about regular expressions read this “How to Find or Validate an Email Address“