Skip to content

Commit ea1b849

Browse files
committed
Implement checkPassowordRules function with real validation
1 parent 3b40e02 commit ea1b849

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

homework/password-check/validation.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <string>
2+
#include <algorithm>
3+
#include <cctype>
24
#include "validation.hpp"
35

46
std::string getErrorMessage(const ErrorCode errorCode) {
@@ -34,7 +36,20 @@ bool doPasswordsMatch(const std::string& password, const std::string& repeatedPa
3436
}
3537

3638
ErrorCode checkPasswordRules(const std::string& password) {
37-
return ErrorCode::Ok;
39+
ErrorCode errorCode{ErrorCode::Ok};
40+
if (password.size() < 9) {
41+
errorCode = ErrorCode::PasswordNeedsAtLeastNineCharacters;
42+
}
43+
if (std::none_of(password.cbegin(), password.cend(), [](const char c){return std::isdigit(c);})) {
44+
errorCode = ErrorCode::PasswordNeedsAtLeastOneNumber;
45+
}
46+
if (std::none_of(password.cbegin(), password.cend(), [](const char c){return std::ispunct(c);})) {
47+
errorCode = ErrorCode::PasswordNeedsAtLeastOneSpecialCharacter;
48+
}
49+
if (std::none_of(password.cbegin(), password.cend(), [](const char c){return std::isupper(c);})) {
50+
errorCode = ErrorCode::PasswordNeedsAtLeastOneUppercaseLetter;
51+
}
52+
return errorCode;
3853
}
3954

4055
ErrorCode checkPassword(const std::string& password, const std::string& repeatedPassowrd) {

0 commit comments

Comments
 (0)