Verifty that a valid email address entered?

Apr 14, 2008 at 6:35pm
I'm working on a school project and it's driving me nuts. The program requires taht a user enteres their email address, name, age etc..... the part I'm having a problem with is that we have to verify the user entered @ in the email entry. if they don't enter the @ and hit submit it must display a message that a valid email is required. The only thing I can't get to work, or figure out how to work is being able to verify the user put the @ in their entry.

Can anybody help me out?

I tried to search for help, but I get things like "email addres is required when registering for this site"....I'm not really sure what to even search for to get a good stepping stone.

BTW - I'm using C++ code for the program

Thanks
Last edited on Apr 14, 2008 at 6:35pm
Apr 14, 2008 at 7:29pm
Well, if you are looking at a string...you could iterate through it and look for an @ symbol like this:

1
2
3
4
5
6
bool checker(string input) {
     for(int a = 0; a < input.size(); a++) {
          if(input.at(a) = '@') return true;
     }
     return false;
}


Or you could make a more dynamic function that wants the symbol to check for as well:

1
2
3
4
5
6
bool checker2(string input, char what) {
     for(int a = 0; a < input.size(); a++) {
          if(input.at(a) = what) return true;
     }
     return false;
}
Last edited on Apr 14, 2008 at 7:29pm
Apr 14, 2008 at 8:42pm
How about...
1
2
3
4
bool containsAtSign(const std::string& s)
{
   return s.find("@") != std::string::npos;
}
Last edited on Apr 14, 2008 at 8:43pm
Apr 15, 2008 at 2:34am
well here is part of my issue. I'm using C++ for a cgi program. so I have my data coming in from an HTML page. so when the data comes in, its not coming in as a string and I'm having problems converting it over to a string. I think both of your suggestions would work, but Now I gotta figure out how to convert the char to a string, then I could search it properly?

thanks for the help
Apr 15, 2008 at 7:15am
>> I'm using C++ for a cgi program. so I have my data coming in from an html >> page. so when the data comes in, its not coming in as a string

is it coming as (char*) or what?

if so,

use this code
1
2
3
4
5
6

  char str[] = "datafrom@htmlpage";
  char * pch;
  pch=strrchr(str,'@');
  return (*pch)?true:false;   



or use javascript code to check if it is valid email or not, at client site
usually everyone uses javascript for that kinda checks



Last edited on Apr 15, 2008 at 7:20am
Apr 15, 2008 at 11:33am
SteakRider
thank you very much, I'm at work but that looks like it will do exactly what I need it to do, thank you for that post. I will try it tonight when I get home and post my results but it looks like it should do the trick
Apr 15, 2008 at 2:02pm
Just for info: std::string has an implicit conversion constructor taking a const char*. Thus the following is valid:
1
2
3
4
5
6
7
8
9
10
bool containsAtSign(const std::string& s)
{
   return s.find("@") != std::string::npos;
}

int main()
{
   const char* foo = "some_text@fish.com";
   containsAtSign(foo);
}


Anyway, SteakRider has given you a direct solution so this is just an aside really.
Last edited on Apr 15, 2008 at 2:39pm
Topic archived. No new replies allowed.