Click here to Skip to main content
15,901,283 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.7K   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 
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.