-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
48 lines (38 loc) · 1.13 KB
/
main.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
const form = document.querySelector('form')
const email = document.querySelector('#email')
const label = email.parentNode
const helperText = document.querySelector('#helper')
const getErrorMessage = (text) => {
const hasAtSign = text.includes('@')
if(text === '') {
return('Error you must provide an email')
}
if(!hasAtSign) {
return('Error you must add the "@"')
}
if(hasAtSign) {
const [before, after] = text.split('@')
if(before === '' || after === '') {
return('Error the email format is not valid')
}
}
return null
}
const displayError = (text) => {
if(text !== null) {
label.classList.replace('border','border-4')
label.classList.replace('border-red-300','border-red-400')
}
else {
label.classList.replace('border-4','border')
label.classList.replace('border-red-400','border-red-300')
}
}
form.addEventListener('submit', (e) => {
e.preventDefault()
const formData = Object.fromEntries(new FormData(form))
const emailText = formData.email
const verifiedText = getErrorMessage(emailText)
displayError(verifiedText)
helperText.textContent = verifiedText
})