Click here to Skip to main content
15,915,328 members
Articles / Desktop Programming / MFC
Article

Validating E-Mails & Phone Number

Rate me:
Please Sign up or sign in to vote.
1.86/5 (7 votes)
21 Feb 2002 85.8K   17   11
Routines to validate E-Mail addresses and Phone numbers

Introduction

Have ever had a field on a form for a users E-Mail address or phone number and the user put garbage values in?

I've written several programs that send E-Mails and Faxes using the information provided by user input. After spending 2 days once fixing blatant typos ie Someuser At Company dot Com, I decided to write a validation routine.

They don't stop all errors, but they do reduce user accidents.

BOOL ValidateEMail(CString Address)
{
	BOOL	RtnVal = TRUE;
//	Address = "A@b.c";    // Valid
//	Address = "A@b.cnet"; // Invalid
//	Address = "A@b.c_";   // Invalid
//	Address = "A@bcnn";   // Invalid
//	Address = "Ab.cnn";   // Invalid
//	Address = "A@bc";     // Invalid
//	Address = "A@bat@.cnn";  // Invalid
//	Address = "A@bat/.com";  // Invalid

	if(Address.GetLength() < 5)
	{
		// Too short
		RtnVal = FALSE;
	}
	else if(Address.Find("@") == -1)		// Has at least 1 @
	{
		RtnVal = FALSE;
	}
	else if(Address.Find(".") == -1)		// Has a Period
	{
		RtnVal = FALSE;
	}
	else if(Address.GetLength() - Address.ReverseFind('.') > 4)		// no more than 3 characters after the final period (reverse find is 0 based not 1 based)
	{
		RtnVal = FALSE;
	}
	else if(Address.ReverseFind('_') > Address.ReverseFind('@'))	// an underscore after the @
	{
		RtnVal = FALSE;
	}
	else
	{
		// only 1 @
		int		FindPos;
		FindPos = Address.Find("@");
		FindPos = Address.Find("@", FindPos+1);
		if(FindPos != -1)
		{
			RtnVal = FALSE;
		}

		// allowed characters 0-9 A-Z _.@-
		int		Pos;
		int		NumChars;

		NumChars = Address.GetLength();
		Address.MakeUpper();

		for(Pos = 0; Pos < NumChars; Pos ++)
		{
			if(
				(!isdigit(Address[Pos]))
				&& ((Address[Pos] < 'A') || (Address[Pos] > 'Z'))
				&& (Address[Pos] != '_')
				&& (Address[Pos] != '.')
				&& (Address[Pos] != '@')
				&& (Address[Pos] != '-'))
			{
				RtnVal = FALSE;
			}
		}
	}
	return RtnVal;
}

BOOL ValidatePhone(CString FaxNum)
{
	BOOL		RtnVal = TRUE;

	if(FaxNum.GetLength() != 11)
	{
		RtnVal = FALSE;
	}
	else
	{
		int		Pos;
		int		NumChars;

		NumChars = FaxNum.GetLength();
		FaxNum.MakeUpper();

		for(Pos = 0; Pos < NumChars; Pos ++)
		{
			if(!isdigit(FaxNum[Pos]))
			{
				if(!isspace(FaxNum[Pos]))
					RtnVal = FALSE;
			}
		}
	}
	return RtnVal;
}

Issues

The e-mail rules will need to change when the new domain names become available

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect TRAC Intermodal
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBUG: Please try "ab@.c" Pin
cwlstudio21-Dec-04 21:39
cwlstudio21-Dec-04 21:39 
GeneralLet's use Automate Pin
blongtq3-Sep-03 0:22
blongtq3-Sep-03 0:22 
GeneralRFC 822 Pin
ZoogieZork8-Jun-03 17:12
ZoogieZork8-Jun-03 17:12 
GeneralInternationalization Pin
PhR18-Feb-03 7:51
PhR18-Feb-03 7:51 
It would be very interesting to get more of these lines. Programmers often need such tools. Good work, even if I've some remarks.

I propose to replace some lines with the next. I've found some things than can help. But there is a big problem, with phone numbers, in France we use 10 numbers, when I look up your code the phone number buffer can have 11 characters. I propose to get the current language ID and to retrieve the phone number length with a new function. Perhaps Windows already has this. I've posted a question today on "Ask to pro", and am waiting...

Thanks
Philippe RIO - phr39@wanadoo.fr
*****************************************************************************************************************************
#include "stdafx.h"

BOOL ValidateEMail(CString Address)
{
// Address = "A@b.c"; // Valid
// Address = "A@b.cnet"; // Invalid
// Address = "A@b.c_"; // Invalid
// Address = "A@bcnn"; // Invalid
// Address = "Ab.cnn"; // Invalid
// Address = "A@bc"; // Invalid
// Address = "A@bat@.cnn"; // Invalid
// Address = "A@bat/.com"; // Invalid

register int iPos ;
register int iNumChars ;

iNumChars = Address.GetLength() ;

if(iNumChars < 5) return (FALSE) ; // Too Short
if(Address.Find("@") == -1) return (FALSE) ; // Has at least 1 @
if(Address.Find(".") == -1) return (FALSE) ; // Has a Period
if(iNumChars - Address.ReverseFind('.') > 4) return (FALSE) ; // no more than 3 characters after the final period (reverse find is 0 based not 1 based)
if(Address.ReverseFind('_') > Address.ReverseFind('@')) return (FALSE) ; // an underscore after the @

// only 1 @

iPos = Address.Find("@") ;
if(Address.Find("@", iPos + 1) != -1) return (FALSE) ;

// allowed characters 0-9 A-Z _.@-

char c ;

iNumChars = Address.GetLength();
Address.MakeUpper();

for(iPos = 0 ; iPos < iNumChars ; iPos++)
{
c = Address[iPos] ;

if((!isalnum(c)) && (c != '_') && (c != '.') && (c != '@') && (c != '-'))
return (FALSE) ;
}

return (TRUE) ;
}

//*************************************************************************************************
//************************* useful code form MSDN *************************************************
//*************************************************************************************************

// This function detects a correct initial UI language for all
// platforms (Win9x, ME, NT4, Windows 2000, Windows XP)
LANGID DetectLanguage()
{
#define MAX_KEY_BUFFER 80

OSVERSIONINFO VersionInfo;
LANGID uiLangID = 0;
HKEY hKey;
DWORD Type, BuffLen = MAX_KEY_BUFFER;
TCHAR LangKeyValue[MAX_KEY_BUFFER];

VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if( !GetVersionEx(&VersionInfo) )
return(0);

switch( VersionInfo.dwPlatformId ) {
// On Windows NT, Windows 2000 or higher
case VER_PLATFORM_WIN32_NT:
if( VersionInfo.dwMajorVersion >= 5) // Windows 2000 or higher
uiLangID = GetUserDefaultUILanguage();
else { // for NT4 check the language of ntdll.dll
uiLangID = GetNTDLLNativeLangID();
if (uiLangID == 1033) { // special processing for Honkong SAR version of NT4
if (IsHongKongVersion()) {
uiLangID = 3076;
}
}
}
break;

// On Windows 95, Windows 98 or Windows ME
case VER_PLATFORM_WIN32_WINDOWS:
// Open the registry key for the UI language
if( RegOpenKeyEx(HKEY_CURRENT_USER,_T("Default\\Control Panel\\Desktop\\ResourceLocale"), 0,
KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS ) {
// Get the type of the default key
if( RegQueryValueEx(hKey, NULL, NULL, &Type, NULL, NULL) == ERROR_SUCCESS
&& Type == REG_SZ ) {
// Read the key value
if( RegQueryValueEx(hKey, NULL, NULL, &Type, (LPBYTE)LangKeyValue, &BuffLen)
== ERROR_SUCCESS ) {
uiLangID = _ttoi(LangKeyValue);
}
}
RegCloseKey(hKey);
}
break;
}

if (uiLangID == 0)
{
uiLangID = GetUserDefaultLangID();
}

// Return the found language ID.
return (uiLangID);
}

//*************************************************************************************************
//************************* useful code form MSDN (end) *******************************************
//*************************************************************************************************

int PerCountryGetPhoneLength(void)
{
switch(GetUserDefaultLangID())
{
case 0x040c : return (10) ; // French (Standard)
case 0x0409 : return (11) ; // English (United States)
.
.
.
default : return (0) ;
}

return (0) ;
}

BOOL ValidatePhone(CString FaxNum)
{
int iPhoneLen ;
char c ;

int iPos, iLast ;
int iNumChars ;

iNumChars = FaxNum.GetLength() ;
iPhoneLen = PerCountryGetPhoneLength() ;

if(iNumChars != iPhoneLen) return (FALSE) ;

FaxNum.MakeUpper() ;

iLast = 0 ;

for(iPos = 0 ; iPos < NumChars ; iPos ++)
{
c = FaxNum[iPos] ;

if(!isdigit(c))
{
if(!isspace(c)) return (FALSE) ;
if(!iPos) continue ;

if(FaxNum[iLast] == ' ') return (FALSE) ;
iLast++ ;
}
}

return (TRUE) ;
}
Generalchar limitations invalid Pin
3-Jun-02 9:28
suss3-Jun-02 9:28 
GeneralRegular Expressions Make It Easy Pin
perlmunger22-Feb-02 17:17
perlmunger22-Feb-02 17:17 
GeneralRe: Regular Expressions Make It Easy Pin
KarstenK10-Apr-02 4:37
mveKarstenK10-Apr-02 4:37 
GeneralRe: Regular Expressions Make It Easy Pin
perlmunger10-Apr-02 5:14
perlmunger10-Apr-02 5:14 
GeneralRe: Regular Expressions Make It Easy Pin
David Crow24-Nov-03 7:15
David Crow24-Nov-03 7:15 
GeneralRe: Regular Expressions Make It Easy Pin
perlmunger24-Nov-03 9:09
perlmunger24-Nov-03 9:09 
GeneralNew TLDs are already being used Pin
Michael Dunn22-Feb-02 10:44
sitebuilderMichael Dunn22-Feb-02 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.