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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carolinefox
    New Member
    • May 2013
    • 1

    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.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    try this link

    Comment

    • Sherin
      New Member
      • Jan 2020
      • 77

      #3
      Try This Code

      Code:
      #include <bits/stdc++.h> 
      using namespace std; 
      
      bool isChar(char c) 
      { 
      	return ((c >= 'a' && c <= 'z') 
      			|| (c >= 'A' && c <= 'Z')); 
      } 
      
      bool isDigit(const char c) 
      { 
      	return (c >= '0' && c <= '9'); 
      } 
      
      bool is_valid(string email) 
      { 
      	
      	if (!isChar(email[0])) { 
      
      		return 0; 
      	} 
      	
      	int At = -1, Dot = -1; 
      
      	for (int i = 0; 
      		i < email.length(); i++) { 
      
      		
      		if (email[i] == '@') { 
      
      			At = i; 
      		} 
      
      		else if (email[i] == '.') { 
      
      			Dot = i; 
      		} 
      	} 
      
      	if (At == -1 || Dot == -1) 
      		return 0; 
      
      	if (At > Dot) 
      		return 0; 
      
      	return !(Dot >= (email.length() - 1)); 
      } 
      
      int main() 
      { 
      	string email = "contribute@geeksforgeeks.org"; 
      
      	bool ans = is_valid(email); 
      
      	if (ans) { 
      		cout << email << " : "
      			<< "valid" << endl; 
      	} 
      	else { 
      		cout << email << " : "
      			<< "invalid" << endl; 
      	} 
      
      	return 0; 
      }

      Comment

      Working...