-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
178 lines (145 loc) · 4.64 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const form = document.getElementById('form')
const username = document.getElementById('username')
const email = document.getElementById('email')
const password = document.getElementById('password')
const password2 = document.getElementById('password2')
let msgError = document.querySelector('#msgError')
let msgSuccess = document.querySelector('#msgSuccess')
let validUsername = false
let validEmail = false
let validPassword = false
let validPassword2 = false
form.addEventListener('submit', (e) => {
e.preventDefault()
isValid()
checkInputs()
})
username.addEventListener('keyup', () =>{
if(username.value.length < 6 || username.value === ''){
setErrorFor(username, 'Usuario deve possuir no mínimo 6 caracteres!')
validUsername = false
} else{
setSuccessFor(username)
validUsername = true
}
})
username.addEventListener('blur', () =>{
if(username.value === ''){
setErrorFor(username, 'Usuário não pode ser vazio!')
validUsername = false
}
})
email.addEventListener('keyup', () =>{
if(!isEmail(email.value) || email.value === ''){
setErrorFor(email, 'Digite um email válido!')
validEmail = false
}else{
setSuccessFor(email)
validEmail = true
}
})
email.addEventListener('blur', () =>{
if(email.value === ''){
setErrorFor(email, 'Email não pode ser vazio!')
validEmail = false
}
})
password.addEventListener('keyup', () =>{
if(password.value.length < 8 || password.value === ''){
setErrorFor(password, 'Senha deve ter no mínimo 8 caracteres!')
validPassword = false
}else{
setSuccessFor(password)
validPassword = true
}
})
password.addEventListener('blur', () =>{
if(password.value === ''){
setErrorFor(password, 'Senha não pode ser vazia!')
validPassword = false
}
})
password2.addEventListener('keyup', () =>{
if(password2.value != password.value || password2.value === ''){
setErrorFor(password2, 'Senhas não correspondem!')
validPassword2 = false
}
else{
setSuccessFor(password2)
validPassword2 = true
}
})
function checkInputs(){
//Dá um get nos inputs
const usernameValue = username.value.trim()
const emailValue = email.value.trim()
const passwordValue = password.value.trim()
const password2Value = password2.value.trim()
if(usernameValue === ''){
setErrorFor(username, 'Usuário não pode ser vazio!')
} else{
setSuccessFor(username)
}
if(emailValue === ''){
setErrorFor(email, 'Email não pode ser vazio!')
} else if(!isEmail(emailValue)){
setErrorFor(email, 'Email não é valido!')
} else{
setSuccessFor(email)
}
if(passwordValue === ''){
setErrorFor(password, 'Senha não pode ser vazia!')
}else{
setSuccessFor(password)
}
if(password2Value === ''){
setErrorFor(password2, 'Senha não pode ser vazia!')
}
else if(passwordValue !== password2Value){
setErrorFor(password2, 'As senhas não correspondem!')
}
else{
setSuccessFor(password2)
}
}
password2.addEventListener('blur', () =>{
if(password2.value === ''){
setErrorFor(password2, 'Senha não pode ser vazia!')
validPassword2 = false
}
})
function isValid(){
if(validUsername && validEmail && validPassword && validPassword2){
msgSuccess.setAttribute('style', 'display: block')
msgSuccess.innerHTML = 'Enviado com sucesso!'
msgError.setAttribute('style', 'display: none')
msgError.innerHTML = ''
setTimeout(() =>{
window.location.reload()
}, 2000)
}else{
msgError.setAttribute('style', 'display: block')
msgError.innerHTML = 'Erro ao cadastrar!'
setTimeout(() =>{
msgError.setAttribute('style', 'display: none')
}, 2000)
msgSuccess.setAttribute('style', 'display: none')
msgSuccess.innerHTML = ''
}
}
function setErrorFor(input, message){
const formControl = input.parentElement //.form-control
const small = formControl.querySelector('small')
//Adiciona a mensagem de erro dentro do small text
small.innerText = message
//Adiciona a classe do erro
formControl.className = 'form-control error'
}
function setSuccessFor(input){
const formControl = input.parentElement
formControl.className = 'form-control success'
}
//Regex
function isEmail(email) {
return /^(([^<>()\[\]\\.,;:\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,}))$/.test(email);
}