-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (46 loc) · 1.66 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const email = document.querySelector('#email');
const phone = document.querySelector('#phone');
const password = document.querySelector('#password');
const confirmPassword = document.querySelector('#confirm-password');
const errorMessage = document.querySelector('.error');
const inputs = [email, phone, password, confirmPassword];
inputs.forEach((item) => {
item.addEventListener('focusin', () => {
errorMessage.textContent = "";
item.classList.remove('error');
if(item == password || item == confirmPassword){
password.classList.remove('error');
}
})
}
);
//if you don't prevent default action, when you submit it will refresh the page. It prevent the page from being refreshed.
const submit = (e) => {
e.preventDefault();
if(password.value != confirmPassword.value){
password.classList.add('error');
confirmPassword.classList.add('error');
errorMessage.textContent = "Passwords don't match!";
return;
}
if(!phone.value.match (
/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/
)
){
phone.classList.add('error');
errorMessage.textContent = "Please enter a valid Phone number!"
return;
}
if(password.value.length < 8){
password.classList.add('error');
errorMessage.textContent = "Password needs to be atleast 8 characters long."
return;
}
errorMessage.textContent = "Form added successfully!";
errorMessage.style.color = "darkgreen";
setTimeout(() => {
window.location.reload()
}, 500)
};
const form = document.querySelector('form');
form.addEventListener('submit', submit);