Click here to Skip to main content
Licence 
First Posted 21 Feb 2002
Views 66,714
Bookmarked 18 times

Validating E-Mails & Phone Number

By | 21 Feb 2002 | Article
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

About the Author

Brad Bruce

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBUG: Please try "ab@.c" Pinmembercwlstudio21:39 21 Dec '04  
GeneralLet's use Automate Pinmemberblongtq0:22 3 Sep '03  
GeneralRFC 822 PinmemberMichael Imamura17:12 8 Jun '03  
GeneralInternationalization PinmemberPhR7:51 18 Feb '03  
Generalchar limitations invalid PinmemberAnonymous9:28 3 Jun '02  
GeneralRegular Expressions Make It Easy Pinmemberperlmunger17:17 22 Feb '02  
GeneralRe: Regular Expressions Make It Easy PinmemberKarstenK4:37 10 Apr '02  
GeneralRe: Regular Expressions Make It Easy Pinmemberperlmunger5:14 10 Apr '02  
GeneralRe: Regular Expressions Make It Easy PinmemberDavidCrow7:15 24 Nov '03  
GeneralRe: Regular Expressions Make It Easy Pinmemberperlmunger9:09 24 Nov '03  
GeneralNew TLDs are already being used PinmemberMichael Dunn10:44 22 Feb '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 22 Feb 2002
Article Copyright 2002 by Brad Bruce
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid