forked from stutitiwari23/Resume-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
264 lines (228 loc) · 7.34 KB
/
validation.js
File metadata and controls
264 lines (228 loc) · 7.34 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// validation.js - Form validation utilities
/**
* Validation utility functions
*/
const Validator = {
/**
* Validates email format
* @param {string} email - Email address to validate
* @returns {boolean} - True if valid, false otherwise
*/
isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
},
/**
* Validates phone number format (supports multiple formats)
* @param {string} phone - Phone number to validate
* @returns {boolean} - True if valid, false otherwise
*/
isValidPhone(phone) {
// Supports formats: +1-234-567-8900, (123) 456-7890, 123-456-7890, 1234567890
const phoneRegex = /^[+]?[(]?[0-9]{1,4}[)]?[-\s.]?[(]?[0-9]{1,4}[)]?[-\s.]?[0-9]{1,9}$/;
return phoneRegex.test(phone);
},
/**
* Validates LinkedIn profile URL
* @param {string} url - LinkedIn URL to validate
* @returns {boolean} - True if valid, false otherwise
*/
isValidLinkedIn(url) {
const linkedinRegex = /^https:\/\/(www\.)?linkedin\.com\/in\/[a-zA-Z0-9_-]+\/?$/;
return linkedinRegex.test(url);
},
/**
* Validates GitHub profile URL
* @param {string} url - GitHub URL to validate
* @returns {boolean} - True if valid, false otherwise
*/
isValidGitHub(url) {
const githubRegex = /^https:\/\/(www\.)?github\.com\/[a-zA-Z0-9_-]+\/?$/;
return githubRegex.test(url);
},
isValidURL(url) {
if (!url) {
return false;
}
// Use Sanitizer's URL validation for consistency
if (typeof Sanitizer !== 'undefined' && Sanitizer.validateURL) {
const result = Sanitizer.validateURL(url);
return result.isValid;
}
// Fallback validation
try {
// Block dangerous protocols
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:', 'file:'];
const lowerUrl = url.toLowerCase();
if (dangerousProtocols.some(protocol => lowerUrl.startsWith(protocol))) {
return false;
}
// Check if it's a valid URL (requires protocol)
const urlPattern = /^(https?:\/\/)(\w+\.)+\w+/;
return urlPattern.test(url);
} catch (e) {
return false;
}
},
/**
* Validates password strength
* @param {string} password - Password to validate
* @returns {object} - Object with isValid and strength properties
*/
validatePassword(password) {
const result = {
isValid: false,
strength: 'weak',
message: '',
requirements: {
minLength: password.length >= 8,
hasUpperCase: /[A-Z]/.test(password),
hasLowerCase: /[a-z]/.test(password),
hasNumber: /[0-9]/.test(password),
hasSpecialChar: /[!@#$%^&*(),.?":{}|<>]/.test(password)
}
};
const metRequirements = Object.values(result.requirements).filter(Boolean).length;
if (password.length < 8) {
result.strength = 'weak';
result.message = 'Password must be at least 8 characters';
} else if (metRequirements < 3) {
result.strength = 'weak';
result.message = 'Weak password';
} else if (metRequirements === 3) {
result.strength = 'medium';
result.message = 'Medium strength password';
result.isValid = true;
} else {
result.strength = 'strong';
result.message = 'Strong password';
result.isValid = true;
}
return result;
},
/**
* Checks if a field is empty
* @param {string} value - Value to check
* @returns {boolean} - True if empty, false otherwise
*/
isEmpty(value) {
return !value || value.trim() === '';
}
};
/**
* UI Helper functions for displaying validation errors
*/
const ValidationUI = {
/**
* Shows an error message for a specific input field
* @param {string} inputId - ID of the input element
* @param {string} message - Error message to display
*/
showError(inputId, message) {
const input = document.getElementById(inputId);
if (!input) {
return;
}
// Remove any existing error
this.clearError(inputId);
// Add error class to input
input.classList.add('input-error');
// Create and insert error message
const errorDiv = document.createElement('div');
errorDiv.className = 'error-message';
errorDiv.id = `${inputId}-error`;
errorDiv.textContent = message;
errorDiv.setAttribute('role', 'alert');
// Insert after the input
input.parentNode.insertBefore(errorDiv, input.nextSibling);
},
/**
* Clears error message for a specific input field
* @param {string} inputId - ID of the input element
*/
clearError(inputId) {
const input = document.getElementById(inputId);
if (!input) {
return;
}
input.classList.remove('input-error');
const errorDiv = document.getElementById(`${inputId}-error`);
if (errorDiv) {
errorDiv.remove();
}
},
/**
* Shows a success indicator for a specific input field
* @param {string} inputId - ID of the input element
*/
showSuccess(inputId) {
const input = document.getElementById(inputId);
if (!input) {
return;
}
this.clearError(inputId);
input.classList.add('input-success');
// Remove success class after animation
setTimeout(() => {
input.classList.remove('input-success');
}, 2000);
},
/**
* Shows password strength indicator
* @param {string} inputId - ID of the password input
* @param {object} result - Password validation result
*/
showPasswordStrength(inputId, result) {
const input = document.getElementById(inputId);
if (!input) {
return;
}
// Remove existing strength indicator
const existingIndicator = document.getElementById(`${inputId}-strength`);
if (existingIndicator) {
existingIndicator.remove();
}
// Create strength indicator
const strengthDiv = document.createElement('div');
strengthDiv.className = `password-strength password-strength-${result.strength}`;
strengthDiv.id = `${inputId}-strength`;
const strengthBar = document.createElement('div');
strengthBar.className = 'password-strength-bar';
const strengthText = document.createElement('span');
strengthText.className = 'password-strength-text';
strengthText.textContent = result.message;
strengthDiv.appendChild(strengthBar);
strengthDiv.appendChild(strengthText);
// Insert after the input
input.parentNode.insertBefore(strengthDiv, input.nextSibling);
},
/**
* Shows a toast notification
* @param {string} message - Message to display
* @param {string} type - Type of toast (success, error, info)
*/
showToast(message, type = 'info') {
// Remove existing toast
const existingToast = document.querySelector('.toast-notification');
if (existingToast) {
existingToast.remove();
}
const toast = document.createElement('div');
toast.className = `toast-notification toast-${type}`;
toast.textContent = message;
toast.setAttribute('role', 'alert');
document.body.appendChild(toast);
// Trigger animation
setTimeout(() => toast.classList.add('show'), 10);
// Remove after 3 seconds
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
};
// Export for use in other files
if (typeof window !== 'undefined') {
window.Validator = Validator;
window.ValidationUI = ValidationUI;
}