-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccountinfovalidator.cpp
126 lines (108 loc) · 3.79 KB
/
accountinfovalidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "accountinfovalidator.h"
#include <QDebug>
const int USERNAME_MIN = 8;
const int USERNAME_MAX = 20;
const int PASSWORD_MIN = 8;
const int PASSWORD_MAX = 16;
AccountInfoValidator::AccountInfoValidator()
{
initialize_validation_variables(false);
}
bool AccountInfoValidator::get_username_status(){
return valid_username;
}
bool AccountInfoValidator::get_email_status(){
return valid_email;
}
bool AccountInfoValidator::get_password1_status(){
return valid_password1;
}
bool AccountInfoValidator::get_password2_status(){
return valid_password2;
}
bool AccountInfoValidator::info_valid_status()
{
return valid_email && valid_username && valid_password1 && valid_password2;
}
QString AccountInfoValidator::get_error_msg(){
return error_msg;
}
void AccountInfoValidator::initialize_validation_variables(bool set_false){
valid_email = set_false;
valid_username = set_false;
valid_password1 = set_false;
valid_password2 = set_false;
}
bool AccountInfoValidator::validate_email(QString email, bool check_empty)
{
if(check_empty && email.isEmpty()){
error_msg = "Email cannot be blank";
return false;
}
QRegExp email_regex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
email_regex.setCaseSensitivity(Qt::CaseInsensitive);
email_regex.setPatternSyntax(QRegExp::RegExp);
error_msg = "Email is not valid";
return valid_email = email_regex.exactMatch(email);
}
bool AccountInfoValidator::validate_username(QString username, bool check_empty)
{
QRegExp username_regex("([\\d]+|[\\w]+)+");
username_regex.setCaseSensitivity(Qt::CaseSensitive);
username_regex.setPatternSyntax(QRegExp::RegExp);
bool regex_valid = username_regex.exactMatch(username);
bool size_valid = username.size() > USERNAME_MIN && username.size() < USERNAME_MAX;
if(check_empty && username.isEmpty()){
error_msg = "Username cannot be blank";
}
else if(size_valid && regex_valid){
return valid_username = true;
}
if(!regex_valid){
error_msg = "Username can contain only alphanumeric characters and unserscores";
}
else if(username.size() < USERNAME_MIN)
{
QString min = QString::number(USERNAME_MIN);
error_msg = "Username must be more than " + min + " characters";
}
else if(username.size() > USERNAME_MAX)
{
QString max = QString::number(USERNAME_MIN);
error_msg = "Username cannot exceed " + max + " characters";
}
return valid_username = false;
}
bool AccountInfoValidator::validate_password(QLineEdit* current_line, QLineEdit* other_line, bool pwd, bool check_empty)
{
QString current_password = current_line->text();
QString other_password = other_line->text();
bool valid;
if(check_empty && (current_password.isEmpty() || other_password.isEmpty())){
error_msg = "Passowrd cannot be blank";
}
else if(!current_password.isEmpty() && !other_password.isEmpty() && current_password == other_password){
valid_password1 = valid_password2 = true;
return true;
}
else if(!current_password.isEmpty() && current_password.size() < PASSWORD_MIN)
{
QString min = QString::number(PASSWORD_MIN);
error_msg = "Password cannot be less than " + min + " characters";
valid = false;
}
else if(!current_password.isEmpty() && current_password.size() > PASSWORD_MAX)
{
QString max = QString::number(PASSWORD_MAX);
error_msg = "Password cannot exceed " + max + " characters";
valid = false;
}
else if(!other_password.isEmpty()){
error_msg = "Passwords do not match";
valid = false;
}
if(pwd){ // Determines which password ui line is currently being updated
return valid_password1 = valid;
}
return valid_password2 = valid;
}