-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
35 lines (31 loc) · 914 Bytes
/
script.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
const form = document.getElementById('form');
const email = document.getElementById('email');
const toastmsg = document.querySelector('.toast-success');
email.addEventListener('input', activeValidation)
form.addEventListener('submit', validation)
// Actively checks the user input
function activeValidation(e) {
if (email.validity.valid) {
form.classList.remove('error');
} else {
validationError();
}
}
// Check when clicked submit
function validation(e) {
e.preventDefault();
if(!email.validity.valid) {
validationError();
} else {
toastmsg.classList.add('show');
setTimeout(function() {
toastmsg.classList.remove('show');
},2000)
}
};
// Throws an error if doesn't match the regEx pattern or if its just an empty field
function validationError() {
if(email.validity.patternMismatch || email.validity.valueMissing) {
form.classList.add('error')
}
}