-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (63 loc) · 1.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const form = document.querySelector('.forms');
form.addEventListener('submit', function (event) {
const nome = document.getElementById('nome').value;
const email = document.getElementById('email').value;
const idade = document.getElementById('idade').value;
const cpf = document.getElementById('cpf').value;
function validarNome(nome) {
if (nome === "") {
alert("campo vazio.");
return false;
}
const regex = /^[A-Za-z\s]/;
if (!regex.test(nome)) {
alert("o nome não pode conter números.");
return false;
}
return true;
}
function validarEmail(email) {
if (email === "") {
alert("campo email vazio.");
return false;
}
if (/^[\w]+@[\w]+.[a-zA-Z]+$/.test(email)) {
return true
}
}
function validarIdade(idade) {
if (idade === "" || isNaN(idade) || idade <= 0) {
alert("insira uma idade válida.");
return false;
}
return true;
}
function validarCPF(cpf) {
if (cpf === "") {
alert("campo cpf vazio.");
return false;
}
if (/[a-zA-Z]/.test(cpf)) {
alert("cpf não pode conter letras.");
return false;
}
if (!/^\d{11}$/.test(cpf)) {
alert("cpf deve conter exatos 11 dígitos numéricos.");
return false;
}
return true;
}
if (!validarNome(nome)) return;
if (!validarEmail(email)) return;
if (!validarIdade(idade)) return;
if (!validarCPF(cpf)) return;
const formData = {
nome: nome,
email: email,
idade: idade,
cpf: cpf,
};
const jsonData = JSON.stringify(formData);
console.log(jsonData);
alert(`usuário cadastrado`);
});