Setup codebase
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* File: Validator.cpp
|
||||
* Description: Validates inputs like phone number, email, password
|
||||
* Author: Trenser
|
||||
* Created: 18-May-2026
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include "Validator.h"
|
||||
|
||||
/*
|
||||
* Function: isPhoneNumberValid
|
||||
* Description: Validates whether the given string is a valid phone number.
|
||||
* Parameters:
|
||||
* phoneNumber - string containing the phone number to validate
|
||||
* Returns:
|
||||
* bool - true if the phone number is valid (10 digits, all numeric), false otherwise
|
||||
*/
|
||||
bool util::isPhoneNumberValid(const std::string& phoneNumber)
|
||||
{
|
||||
if (phoneNumber.size() != 10)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return std::all_of(phoneNumber.begin(), phoneNumber.end(),
|
||||
[](char character)
|
||||
{
|
||||
return std::isdigit(character);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: isEmailValid
|
||||
* Description: Validates whether the given string is a properly formatted email address.
|
||||
* Parameters:
|
||||
* email - string containing the email address to validate
|
||||
* Returns:
|
||||
* bool - true if the email contains exactly one '@' character and is not at the start or end, false otherwise
|
||||
*/
|
||||
bool util::isEmailValid(const std::string& email)
|
||||
{
|
||||
size_t index = email.find('@');
|
||||
if (index == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (email.find('@', index + 1) != std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (index == 0 || index == email.size() - 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: isPasswordValid
|
||||
* Description: Validates whether the given string meets password requirements.
|
||||
* Parameters:
|
||||
* password - string containing the password to validate
|
||||
* Returns:
|
||||
* bool - true if the password is valid, false otherwise
|
||||
* Notes:
|
||||
* - Must not equal the default password
|
||||
* - Must be at least 8 characters long
|
||||
* - Must contain at least one uppercase letter, one lowercase letter,
|
||||
* one digit, and one special character
|
||||
* - Must not contain whitespace
|
||||
*/
|
||||
bool util::isPasswordValid(const std::string& password)
|
||||
{
|
||||
if (password.length() < 8)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool hasUpper = false;
|
||||
bool hasLower = false;
|
||||
bool hasDigit = false;
|
||||
bool hasSpecial = false;
|
||||
for (char character : password)
|
||||
{
|
||||
if (std::isspace(static_cast<unsigned char>(character)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (std::isupper(static_cast<unsigned char>(character)))
|
||||
{
|
||||
hasUpper = true;
|
||||
}
|
||||
else if (std::islower(static_cast<unsigned char>(character)))
|
||||
{
|
||||
hasLower = true;
|
||||
}
|
||||
else if (std::isdigit(static_cast<unsigned char>(character)))
|
||||
{
|
||||
hasDigit = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
hasSpecial = true;
|
||||
}
|
||||
}
|
||||
|
||||
return hasUpper && hasLower && hasDigit && hasSpecial;
|
||||
}
|
||||
Reference in New Issue
Block a user