-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (33 loc) · 947 Bytes
/
script.js
File metadata and controls
40 lines (33 loc) · 947 Bytes
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
const form = document.querySelector('#form-js');
const email = document.querySelector('#email-input');
function showError(ele, msg) {
const formControl = ele.parentElement;
const small = formControl.querySelector('small');
small.innerText = msg;
formControl.classList.add('error');
}
function validate() {
const emailValue = email.value.trim();
function isEmail(email) {
return /^([a-zA-Z0-9_\-\\.]+)@([a-zA-Z0-9_\-\\.]+)\.([a-zA-Z]{2,5})$/.test(email);
}
if (!emailValue) {
showError(email, 'Please provide a valid email address');
} else if (!isEmail(emailValue)) {
showError(email, 'Email is not valid');
}
}
// funtion that removes error
function removeError() {
const formControl = email.parentElement;
formControl.classList.remove('error');
}
function resetInput() {
form.reset();
}
form.addEventListener('submit', (e) => {
e.preventDefault();
removeError();
validate();
resetInput();
});