-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
55 lines (47 loc) · 1.78 KB
/
app.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
53
54
55
let signin_form = document.querySelector('#signin-form')
let signin_btn = document.querySelector('#signin-btn')
validateEmail = (email) => {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(email).toLowerCase())
}
checkSigninInput = (input) => {
let err_span = signin_form.querySelector(`span[data-err-for="${input.id}"]`)
let val = input.value.trim()
let form_group = input.parentElement
switch(input.getAttribute('type')) {
case 'password':
if (val.length < 6) {
form_group.classList.add('err')
form_group.classList.remove('success')
err_span.innerHTML = 'Password must be at least 6 characters'
} else {
form_group.classList.add('success')
form_group.classList.remove('err')
err_span.innerHTML = ''
}
break;
case 'email':
if (val.length === 0 || !validateEmail(val)) {
form_group.classList.add('err')
form_group.classList.remove('success')
err_span.innerHTML = 'Email is invalid'
} else {
form_group.classList.add('success')
form_group.classList.remove('err')
err_span.innerHTML = ''
}
}
}
checkSigninForm = () => {
let inputs = signin_form.querySelectorAll('.form-input')
inputs.forEach(input => checkSigninInput(input))
}
signin_btn.onclick = () => {
checkSigninForm()
}
let inputs = signin_form.querySelectorAll('.form-input')
inputs.forEach(input => {
input.addEventListener('focusout', () => {
checkSigninInput(input)
})
})