Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "InputValidator.h"
- bool InputValidator::isUserNameValid(string userName)
- {
- if (userName.length() >= 3) {
- for each (char c in userName)
- {
- if (!islower(c) && !isupper(c) && !isdigit(c) && c != '_') {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- bool InputValidator::isPasswordValid(string password)
- {
- bool haveLower = false;
- bool haveUpper = false;
- bool haveSpecial = false;
- bool haveDigit = false;
- if (password.length() >= 5) {
- for each (char c in password)
- {
- if (islower(c)) {
- haveLower = true;
- }
- else if (isupper(c)) {
- haveUpper = true;
- }
- else if (isdigit(c)) {
- haveDigit = true;
- }
- else if (c == '!' || c == '@' || c == '#' || c == '$') {
- haveSpecial = true;
- }
- else {
- return false;
- }
- }
- if (haveDigit && haveLower && haveUpper && haveSpecial) {
- return true;
- }
- }
- return false;
- }
- bool InputValidator::isEmailValid(string email)
- {
- int atPosition = email.find('@', 3);
- int dotPosition = email.find('.');
- if (atPosition < 0 || dotPosition < 0 || (dotPosition - atPosition > 6) || (dotPosition - atPosition < 2)) {
- return false;
- }
- for (int i = 0; i < atPosition; i++)
- {
- if (!islower(email[i]) && !isupper(email[i]) && !isdigit(email[i]) && email[i] != '_') {
- return false;
- }
- }
- for (int i = atPosition + 1; i < dotPosition; i++)
- {
- if (!islower(email[i])) {
- return false;
- }
- }
- string domain = email.substr(dotPosition + 1, email.length() - dotPosition);
- if (domain != "com" && domain != "bg" && domain != "net" && domain != "en") {
- return false;
- }
- return true;
- }
- bool InputValidator::isRealNameValid(string name)
- {
- if (name.empty()) {
- return true;
- }
- int firstSpacePosition = name.find_first_of(' ', 1);
- int secondSpacePosition = name.find_first_of(' ', firstSpacePosition + 1);
- if (name.length() > 255 || firstSpacePosition < 0 || secondSpacePosition < 0) {
- return false;
- }
- string firstName = name.substr(0, firstSpacePosition);
- string secondName = name.substr(firstSpacePosition + 1, secondSpacePosition - firstSpacePosition - 1);
- string thirdName = name.substr(secondSpacePosition + 1, name.length() - secondSpacePosition);
- for each (char c in firstName)
- {
- if (!islower(c) && !isupper(c)) {
- return false;
- }
- }
- for each (char c in secondName)
- {
- if (!islower(c) && !isupper(c)) {
- return false;
- }
- }
- for each (char c in thirdName)
- {
- if (!islower(c) && !isupper(c)) {
- return false;
- }
- }
- return true;
- }
- bool InputValidator::isAddressValid(string address)
- {
- if (address.empty()) {
- return true;
- }
- if (address.length() > 255) {
- return false;
- }
- for each (char c in address)
- {
- if (!islower(c) && !isupper(c) && !isdigit(c) && c != ' ') {
- return false;
- }
- }
- return true;
- }
- bool InputValidator::isBirthdateValid(string birthDate)
- {
- if (birthDate.empty()) {
- return true;
- }
- int firstDashPosition = birthDate.find_first_of('-');
- int secondDashPosition = birthDate.find_first_of('-', firstDashPosition + 1);
- if (birthDate.length() != 10 || firstDashPosition != 4 || secondDashPosition != 7) {
- return false;
- }
- for (int i = 0; i < birthDate.length(); i++)
- {
- if (i == firstDashPosition || i == secondDashPosition) {
- continue;
- }
- if (!isdigit(birthDate[i])) {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement