-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (56 loc) · 1.72 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
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
const email = document.getElementById('files-section--email');
const textError = document.querySelector('small');
const form = document.querySelector('.form');
const isEmailValid = (email) => {
const re = /^(([^<>()\[\]\\.,;:\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,}))$/;
return re.test(email);
};
const showError = (input, message) => {
const formField = input.parentElement;
formField.classList.remove('success');
formField.classList.add('error');
textError.classList.toggle('inactive');
const error = formField.querySelector('small');
error.textContent = message;
};
const showSuccess = (input) => {
const formField = input.parentElement;
formField.classList.remove('error');
formField.classList.add('success');
error.textContent = '';
};
const checkEmail = () => {
let valid = false;
const em = email.value.trim();
if (!isEmailValid(em)) {
showError(email, 'Please check your email.')
} else {
showSuccess(email);
valid = true;
}
return valid;
}
form.addEventListener('submit', function (event) {
event.preventDefault();
let isEmailValid = checkEmail();
let isFormValid = isEmailValid;
if (isFormValid) {
// submit to the server if the form is valid...
}
});
const debounce = (fn, delay = 1000) => {
let timeoutId;
return (...args) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
fn.apply(null, args)
}, delay);
};
};
form.addEventListener('input', debounce(function (e) {
if (e.target.id) {
checkEmail();
}
}));