-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (35 loc) · 1.16 KB
/
index.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
const form = document.getElementById('sign-up-form');
const password = document.getElementById('password');
const errorMessage = document.getElementById('error-msg');
const confirmPassword = document.getElementById('confirm-password');
const ERROR_MESSAGE = 'Passwords do not match';
function validatePassword(e) {
const field = e.target;
if (confirmPassword.value && password.value !== confirmPassword.value) {
field.setCustomValidity(ERROR_MESSAGE);
addErrorClass(field);
setErrorMessage(`* ${ERROR_MESSAGE}`);
} else {
password.setCustomValidity('');
confirmPassword.setCustomValidity('');
removeErrorClassInPasswordFields();
setErrorMessage();
}
field.reportValidity();
}
function addErrorClass(field) {
field.classList.add('error');
}
function removeErrorClassInPasswordFields() {
password.classList.remove('error');
confirmPassword.classList.remove('error');
}
function setErrorMessage(msg = '') {
errorMessage.innerText = msg;
}
confirmPassword.addEventListener('input', validatePassword);
password.addEventListener('input', validatePassword);
form.addEventListener('submit', (e) => {
e.preventDefault();
location.reload();
});