-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.js
50 lines (45 loc) · 1.49 KB
/
form.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
// Constraint form validation API
const form = document.querySelector('.form-contact');
const userName = document.querySelector('#name')
const userEmail = document.querySelector('#email');
const userMessage = document.querySelector('#msg');
const message = document.querySelector('small');
// Get the value of the inputs
form.addEventListener('submit', (e) => {
const client = userName.value;
const emailAddress = userEmail.value;
const interestMessage = userMessage.value;
const errorMessage = [];
if (emailAddress === '') {
errorMessage.push('The email value must not be empty');
message.innerHTML = errorMessage.join(', ');
}
if (emailAddress !== emailAddress.toLowerCase()) {
errorMessage.push('The email must be in lower case');
message.innerHTML = errorMessage.join(', ');
}
if (errorMessage.length > 0) {
e.preventDefault();
message.classList.add('error');
message.innerHTML = errorMessage.join(', ');
}
});
/* local storage */
// set Item in the local storage
form.addEventListener('input', () => {
const data = {
'name': userName.value,
'email': userEmail.value,
'message': userMessage.value,
}
const dataStored = JSON.stringify(data);
localStorage.setItem('data', dataStored);
location.reload;
});
// Get item from the local storage.
window.addEventListener('load', () => {
const getData = JSON.parse(localStorage.getItem('data'));
userName.value = getData.name;
userEmail.value = getData.email;
userMessage.value = getData.message;
});