0

for a project, I'm trying to validate my user input email using regex but it seems to be accepting anything I enter. I know I am probably doing something stupid as I am new to C++. Sorry guys! Hope you's can help.

User.cpp file

#include "pch.h"
#include "User.h"

//Email regex
std::string userEmailRegex = "[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a- 
z0-9-]+)*(.[a-z]{2,4})";

//Validates data against a user-defined string
bool validate(std::string regexStr, std::string data)
{
return std::regex_match(data, std::regex(regexStr));
}

User::User()
{
}

User::User(std::string email, std::string password, std::string username)
{
setEmail(email);
setPassword(password);
setUsername(username);
}

User::~User()
{
}

void User::setEmail(std::string email)
{
bool bValid = validate(userEmailRegex, email);
if (bValid)
{
    this->email = email;
}
else
{
    this->email = "default@default.com";
}
}

Main.cpp file

#include "pch.h"
#include "User.h"

User u;

int main()
{
std::vector<User> v;

std::string email = u.getEmail();
std::cout << "Email: ";
std::cin >> email;

}

Casper
  • 29
  • 6
  • 1
    That regular expression isn't even close to correct and invalidates a lot of common email addresses using [TLDs longer than 4 letters](https://data.iana.org/TLD/tlds-alpha-by-domain.txt). Check for an `@`. Attempt delivery. That's how you verify an email address. Everything else is rampant speculation about what ICANN will do next. For "default" email addresses use something like `invalid@nil` not [default.com](http://default.com/) which is actually owned by someone. – tadman Jul 05 '19 at 17:32
  • Also since this is C++ use `const std::string&` for arguments. Using `std::string` creates pointless copies of every single value supplied. – tadman Jul 05 '19 at 17:33
  • Validating a email with a regex is *really complicated* if you want to get all the cornercases correct. If you just want to check if someone entered something that looks like an email I'd just go with ".+@.+" – Jesper Juhl Jul 05 '19 at 17:50

1 Answers1

0

For your regex, your forgot to escape ., else it means any character (except \n).

R"([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4}))"

But it is not the right way to validate email, see can-it-cause-harm-to-validate-email-addresses-with-a-regex.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    This is still categorically wrong and will reject tons of valid emails. This naive regular expression will do more harm than good. – tadman Jul 05 '19 at 17:47
  • @tadman: I agree, I fix OP's intent with non escaping dot. and provide link to explain why validating email via regex is the wrong way. – Jarod42 Jul 05 '19 at 17:52
  • Ken White has already posted a much better suggestion above in the comments. – tadman Jul 05 '19 at 18:07
  • It will allows some funky ones as well, e,g, `not@server@shop.com` is OK per above. – David C. Rankin Jul 05 '19 at 18:59