Email Validation

I have to write a program to validate an email address. To do this I have to use the following criteria:

•The email address must start with a letter (no numbers or symbols)
•There must be an @ somewhere in the string that is located BEFORE the dot
•There must be text after the @ symbol but BEFORE the dot
•There must be a dot
•There must be text after the dot

I have started the program...but I cannot figure out how to use .find correctly.
I know that I have to search for the @ symbol..but I don't know how to make sure that it is before the dot...and make sure there is text before it...and text after the dot...any help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string email;
	

	cout << "Enter in your email and I will tell \n"
		<< "you if it is valid or not. " ;
	getline(cin, email);

	for (int pos = 0; pos < email.find('@', I DONT KNOW WHAT GOES HERE ); pos++)

Last edited on
This looks like a job for regular expressions...

That said, you can try checking the criteria manually, one step at a time. First check to see if the first character is alphabetical. If it is, move on. Second, check to see if there is only one @ in the address. If so, move on. Third, check to see if the character after the @ is not a dot. And so on. Get the idea?
I do get the idea, but I'm not sure how to go about that. I just learned what .find is, and I do not know how to check for things like if something is alphabetical or not.
I'm gonna do the function on the fly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool isCharacter(const char Character)
{
	return ( (Character >= 'a' && Character <= 'z') || (Character >= 'A' && Character <= 'Z'));
	//Checks if a Character is a Valid A-Z, a-z Character, based on the ascii value
}
bool isNumber(const char Character)
{
	return ( Character >= '0' && Character <= '9');
	//Checks if a Character is a Valid 0-9 Number, based on the ascii value
}
bool isValidEmailAddress(const char * EmailAddress)
{
	if(!EmailAddress) // If cannot read the Email Address...
		return 0;
	if(!isCharacter(EmailAddress[0])) // If the First character is not A-Z, a-z
		return 0;
	int AtOffset = -1;
	int DotOffset = -1;
	unsigned int Length = strlen(EmailAddress); // Length = StringLength (strlen) of EmailAddress
	for(unsigned int i = 0; i < Length; i++)
	{
		if(EmailAddress[i] == '@') // If one of the characters is @, store it's position in AtOffset
			AtOffset = (int)i;
		else if(EmailAddress[i] == '.') // Same, but with the dot
			DotOffset = (int)i;
	}
	if(AtOffset == -1 || DotOffset == -1) // If cannot find a Dot or a @
		return 0;
	if(AtOffset > DotOffset) // If the @ is after the Dot
		return 0;
	return !(DotOffset >= ((int)Length-1)); //Chech there is some other letters after the Dot
}

In your program, use it like this:
1
2
std::string TestString = "lol@test.com";
if(isValidEmailAddress( TestString.c_str() ) ...

If you plan on switching from TestString to a char Array, you can easily use it like this:
1
2
char TestString = "lol@test.com";
if(isValidEmailAddress( TestString ) ) ...

It may have some bugs, like, it will say this is a valid email address:
lol@test.°#ç
Last edited on
@EssGeEich: You ought not to just give solutions like that. Too often people just take the work and turn it in as their own, learning nothing.
I see. But in fact, i like to help people, hoping they respect my work, so i'm just going to hope he will learn something about it. I'm gonna add some more comments.
I do not want to take this work...
First off, I do not understand most of it...
Second, taking someone elses work is wrong.

I am trying to learn from it, but I am still confused.
I wanted to work through everything individually, and I sort of understand what you were doing..but not really.
I understand your sentiment, but while you may be helping some people by giving them code, those that learn nothing are actually being hindered. They turn in your program, get a grade, and move on to more difficult concepts they still don't understand because someone always just gave them all the answers.

It's tantamount to, say I come with a question on how to calculate the derivative of x2. If you say, "that's easy" and say the answer is 2x+c, well I can certainly turn that into my teacher and get that problem correct. But then on a test he has 3x2 and I have no idea how to handle because I didn't learn the process. Does that make sense?
If x2 = 2x+c, well, it does.
That's basic calculus. If you haven't had any experience with that, I apologize. I meant to just give an different example of the problem with your method.
Never had such experiences, But i was being sarcastic... I understand what you meant
Alright..I've been trying really hard to understand your code...and here is what I have now. I think it should be working but it is not...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{	string test;
	string email;
	bool isCharacter();
	bool isNumber();
	bool isValidEmailAddress(string);

	cout << "Enter in your email and I will tell \n"
		<< "you if it is valid or not. \n" ;
	getline(cin, email);	

	test=email;

	if(isValidEmailAddress(test) )
		cout << "Your email address is valid.";
	else
		cout << "Your email address is invalid, Please re-enter.";
	getline(cin, email);

	system("pause");
	return 0;
}

	bool isCharacter(const char Character)
{
	return ( (Character >= 'a' && Character <= 'z') || (Character >= 'A' && Character <= 'Z'));
}

bool isNumber(const char Character)
{
	return ( Character >= '0' && Character <= '9');
}

bool isValidEmailAddress(const char * email)
{
	if(!email)
		return 0;
	if(!isCharacter(email[0]))
		return 0;
	int AtOffset = -1;
	int DotOffset = -1;
	unsigned int Length = strlen(email);

	for(unsigned int i = 0; i < Length; i++)
	{
		if(email[i] == '@')
			AtOffset = (int)i;
		else if(email[i] == '.')
			DotOffset = (int)i;
	}

	if(AtOffset == -1 || DotOffset == -1)
		return 0;
	if(AtOffset > DotOffset)
		return 0;
	return !(DotOffset >= ((int)Length-1));

}
Er, what is this for?

As it is, the code so far doesn't even come close to properly validating an email address. The problem is so complex, actually, that the complete, industry-standard, regex for it is actually broken. (By the way, here it is:
http://code.iamcal.com/php/rfc822/full_regexp.txt .)

The standard itself: http://www.faqs.org/rfcs/rfc2822.html

So, if this is a homework, your teacher will have to have given you some constraints he expects you to use, since an email address is such a warped and twisted thing that whatever you code, someone, somewhere will have a valid email address that will not validate with your code.

If this is for business, you will have to consider what exactly you are trying to accomplish. Is it just to make sure that the user inputs something other than rubbish?


The only guaranteed way to test an email address is to send an email and see if it bounces.


Technically, you could also test the target server to see if it accepts that email address, but many servers do not respond to those requests, and if they do they may respond incorrectly.
It is just homework and the only restraints are in my original post. I know that it can accept something completely incorrect.
Ah, then stick to using std::string, like you did originally and avoid c-strings.

The string class provides you with all the methods you need to find stuff in the string. Alternately, you can just loop through the string and check that specific things occur before others.

What your example says is that an email address is something like:

    aluser1337@nowhere.org

Notice that the string is:

  • starts with isalpha(c)==true
  • zero or more of any character except '.' and '@'
  • a '@'
  • one or more of any character except '.' and '@'
  • a '.'
  • one or more of any character except '.' and '@'

[edit] I almost forgot:
http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/clibrary/cctype/

Hope this helps.
Last edited on
About my code not working, you should place
 
int main(...

AFTER my functions.
and delete those lines:
1
2
3
bool isCharacter();
bool isNumber();
bool isValidEmailAddress(string);

And again, remember:
In your program, use it like this:
1
2
std::string TestString = "lol@test.com";
if(isValidEmailAddress( TestString.c_str() ) ...

Last edited on
@EssGeEich
Why are you so determined to do his homework for him? Any competent professor will just fail him for obviously cut-n-pasted code.
Aha, so i'll clearly stop posting code in this topic.
Topic archived. No new replies allowed.