-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactScript.js
More file actions
98 lines (77 loc) · 3.11 KB
/
contactScript.js
File metadata and controls
98 lines (77 loc) · 3.11 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
document.addEventListener('DOMContentLoaded', function () {
const form = document.getElementById('contact-form');
const statusMessage = document.getElementById('status-message');
const allInputs = document.querySelectorAll('#contact-form [required]');
const passwordInput = document.getElementById('password');
const strengthBar = document.getElementById('strength-bar');
const strengthText = document.getElementById('strength-text');
const subjectTextarea = document.getElementById('subject');
const charCounter = document.getElementById('char-counter');
allInputs.forEach(input => {
input.addEventListener('input', () => {
validateField(input);
});
});
function validateField(field) {
if (field.checkValidity()) {
field.classList.remove('error-input');
} else {
field.classList.add('error-input');
}
}
passwordInput.addEventListener('input', () => {
const password = passwordInput.value;
let strength = 0;
let feedback = '';
if (password.length >= 8) strength++;
if (password.match(/[a-z]/)) strength++;
if (password.match(/[A-Z]/)) strength++;
if (password.match(/[0-9]/)) strength++;
if (password.match(/[^a-zA-Z0-9]/)) strength++;
strengthBar.style.width = (strength * 20) + '%';
switch (strength) {
case 0:
case 1:
strengthBar.style.backgroundColor = '#dc3545'; // Red
feedback = 'Weak';
break;
case 2:
strengthBar.style.backgroundColor = '#ffc107'; // Orange
feedback = 'Medium';
break;
case 3:
strengthBar.style.backgroundColor = '#28a745'; // Green
feedback = 'Good';
break;
case 4:
case 5:
strengthBar.style.backgroundColor = '#28a745'; // Strong Green
feedback = 'Strong';
break;
}
strengthText.textContent = `Strength: ${feedback}`;
});
subjectTextarea.addEventListener('input', () => {
const currentLength = subjectTextarea.value.length;
const maxLength = subjectTextarea.getAttribute('maxlength');
charCounter.textContent = `${currentLength} / ${maxLength}`;
});
form.addEventListener('submit', function(event) {
let isFormValid = true;
allInputs.forEach(input => {
validateField(input);
if (!input.checkValidity()) {
isFormValid = false;
}
});
if (isFormValid) {
statusMessage.textContent = 'Validation successful! Opening your email client...';
statusMessage.className = 'success';
} else {
event.preventDefault();
statusMessage.textContent = 'Please fix the errors in the fields highlighted in red.';
statusMessage.className = 'error';
}
});
window.alert("Welcome to the Contact Page!");
});