-
Notifications
You must be signed in to change notification settings - Fork 4
/
validation.js
183 lines (133 loc) · 4.79 KB
/
validation.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function ValidateLoginForm() {
RemoveAllErrorMessage();
var LoginEmail = document.getElementById('LoginEmail').value;
var LoginPassword = document.getElementById('LoginPassword').value;
var PasswordValidationMessage;
var emailValidationMessage;
emailValidationMessage = isValidEmail(LoginEmail);
if(emailValidationMessage != "valid"){
ShowErrorMessage('LoginEmail',emailValidationMessage);
return false;
}
PasswordValidationMessage = isValidPassword(LoginPassword);
if(PasswordValidationMessage != "valid"){
ShowErrorMessage('LoginPassword',PasswordValidationMessage);
return false;
}
return true;
}
function ValidateRegistrationForm(){
RemoveAllErrorMessage();
var RegiName = document.getElementById('RegiName').value;
var RegiEmailAddres = document.getElementById('RegiEmailAddres').value;
var RegiPassword = document.getElementById('RegiPassword').value;
var RegiConfirmPassword = document.getElementById('RegiConfirmPassword').value;
var PasswordValidationMessage;
var ConfirmPasswordMessage;
var emailValidationMessage;
if(RegiName == ""){
ShowErrorMessage('RegiName',"Please fill the filed.");
return false;
}else if(RegiName.length < 3 || RegiName.length > 20){
ShowErrorMessage('RegiName',"Name should be minimum 3 and maximum 20 characters long.");
return false;
}
emailValidationMessage = isValidEmail(RegiEmailAddres);
if(emailValidationMessage != "valid"){
ShowErrorMessage('RegiEmailAddres',emailValidationMessage);
return false;
}
PasswordValidationMessage = isValidPassword(RegiPassword);
if(PasswordValidationMessage != "valid"){
ShowErrorMessage('RegiPassword',PasswordValidationMessage);
return false;
}
ConfirmPasswordMessage = isValidPassword(RegiConfirmPassword);
if(ConfirmPasswordMessage != "valid"){
ShowErrorMessage('RegiConfirmPassword',ConfirmPasswordMessage);
return false;
}
if(RegiPassword != RegiConfirmPassword){
ShowErrorMessage('RegiConfirmPassword',"Password not match.");
return false;
}
return true;
}
function ValidateForgotPasswordForm(){
RemoveAllErrorMessage();
var forgotPassEmail = document.getElementById('forgotPassEmail').value;
var emailValidationMessage;
emailValidationMessage = isValidEmail(forgotPassEmail);
if(emailValidationMessage != "valid"){
ShowErrorMessage('forgotPassEmail',emailValidationMessage);
return false;
}
}
function ValidateResetPasswordForm(){
RemoveAllErrorMessage();
var NewPassword = document.getElementById('NewPassword').value;
var ConfirmNewPassword = document.getElementById('ConfirmNewPassword').value;
var PasswordValidationMessage;
var ConfirmPasswordMessage;
PasswordValidationMessage = isValidPassword(NewPassword);
if(PasswordValidationMessage != "valid"){
ShowErrorMessage('NewPassword',PasswordValidationMessage);
return false;
}
ConfirmPasswordMessage = isValidPassword(ConfirmNewPassword);
if(ConfirmPasswordMessage != "valid"){
ShowErrorMessage('ConfirmNewPassword',ConfirmPasswordMessage);
return false;
}
if(NewPassword != ConfirmNewPassword){
ShowErrorMessage('ConfirmNewPassword',"Password not match.");
return false;
}
return true;
}
function RemoveAllErrorMessage(){
var allErrorMessage = document.getElementsByClassName('error-message');
var allErrorFiled = document.getElementsByClassName('error-input');
var i;
for(i=(allErrorMessage.length - 1); i>=0; i--){
allErrorMessage[i].remove();
}
for(i=(allErrorFiled.length-1);i>=0;i--){
allErrorFiled[i].classList.remove('error-input');
}
}
function ShowErrorMessage(InputBoxID,Message){
var InputBox = document.getElementById(InputBoxID);
InputBox.classList.add('error-input');
InputBox.focus();
var ErrorMessageElement = document.createElement("p");
ErrorMessageElement.innerHTML = Message;
ErrorMessageElement.classList.add('error-message');
ErrorMessageElement.setAttribute("id",InputBoxID+'-error');
InputBox.parentNode.insertBefore(ErrorMessageElement, InputBox.nextSibling);
}
function isValidEmail(email){
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(email == ""){
return "Please fill the field.";
}
if(emailRegex.test(email) == false){
return "This is not a valid email.";
}
return "valid";
}
function isValidPassword(password) {
const minLength = 8;
const maxLength = 32;
const letterNumberRegexSpecialChar = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*])[a-zA-Z\d!@#$%^&*]{8,}$/;
if(password == ""){
return "Please fill the field."
}
if (password.length < minLength || password.length > maxLength) {
return "Password length should be minimum 8 & maximum 32 characters.";
}
if (!letterNumberRegexSpecialChar.test(password)) {
return "Password should contain alphabetic, numeric and special characters.";
}
return "valid";
}