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.
How to perform data validation for emails with C++?
Collapse
X
-
Tags: None
-
-
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
Comment