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;
}