Click here to Skip to main content
15,915,093 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 
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 
KarstenK wrote:
If the program is already linking it maybe okay (but I don´t believe it).

If not, its the biggest bullshit ever to link to library to parse a string. (&& the code is great && M$ rulez)


Why is that bullshit? Hard drives are cheap. Who cares if it makes your exe bigger in the end? It'll run faster and be less likely to have buffer overruns, etc.

Usually the best way to write software is to use the right tool for the job. While using regular expressions for one string in an application may be overkill (you have a point there) and could be done almost as easily writing your own parser, it's certainly not an invalid choice. However, my point was that regular expressions are very useful and you will probably find more than one use for them once you *have* linked it into your project.

Now, if your point has to do with speed or benchmarking, I would gladly take that challenge. You write a parser in straight C, and I'll write one using a regular expression engine such as Regex++. Then we can compare which is the better solution based on three things: 1) the time it takes to create the parser, 2) efficiency, and 3) raw speed. Meanwhile, you should consider being a little more polite when you post a response here.

Just because someone doesn't write code the way *you* do, doesn't make it "the biggest bullshit" (whatever that means)--and it certainly doesn't make your way better. BTW, have you ever used regular expressions? Seems to me that to have an opinion on something, you should probably have used it first.

-Matt


------------------------------------------

The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
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.