467,166 Members | 1,133 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

How to perform data validation for emails with C++?

I have this big project and I need to do data validation for an email. I have googled several stuff but I always get something so not in the topic.
May 15 '13 #1
  • viewed: 2584
Share:
2 Replies
1GB
try this link
May 15 '13 #2
64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. #include <bits/stdc++.h> 
  2. using namespace std; 
  3.  
  4. bool isChar(char c) 
  5.     return ((c >= 'a' && c <= 'z') 
  6.             || (c >= 'A' && c <= 'Z')); 
  7.  
  8. bool isDigit(const char c) 
  9.     return (c >= '0' && c <= '9'); 
  10.  
  11. bool is_valid(string email) 
  12.  
  13.     if (!isChar(email[0])) { 
  14.  
  15.         return 0; 
  16.     } 
  17.  
  18.     int At = -1, Dot = -1; 
  19.  
  20.     for (int i = 0; 
  21.         i < email.length(); i++) { 
  22.  
  23.  
  24.         if (email[i] == '@') { 
  25.  
  26.             At = i; 
  27.         } 
  28.  
  29.         else if (email[i] == '.') { 
  30.  
  31.             Dot = i; 
  32.         } 
  33.     } 
  34.  
  35.     if (At == -1 || Dot == -1) 
  36.         return 0; 
  37.  
  38.     if (At > Dot) 
  39.         return 0; 
  40.  
  41.     return !(Dot >= (email.length() - 1)); 
  42.  
  43. int main() 
  44.     string email = "contribute@geeksforgeeks.org"; 
  45.  
  46.     bool ans = is_valid(email); 
  47.  
  48.     if (ans) { 
  49.         cout << email << " : "
  50.             << "valid" << endl; 
  51.     } 
  52.     else { 
  53.         cout << email << " : "
  54.             << "invalid" << endl; 
  55.     } 
  56.  
  57.     return 0; 
  58.  
3 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Sonia | last post: by
2 posts views Thread by Gustaf Liljegren | last post: by
1 post views Thread by Bill nguyen | last post: by
4 posts views Thread by kids_pro | last post: by
reply views Thread by ss | last post: by
2 posts views Thread by =?Utf-8?B?dGFtZWU=?= | last post: by
2 posts views Thread by KDawg44 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.