-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (82 loc) · 2.73 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const btn = document.querySelector('.btn');
const calcular_imc = (peso, altura, nome, elemento) => {
imc = (peso / (altura * altura)).toFixed(1);
nome = nome.value;
el = document.querySelector(elemento);
function avaliarIMC(imc) {
if (imc < 18.5) {
return el.innerHTML = nome + ' o seu IMC é '+ imc +' e Você esta abaixo do peso.';
} else if (imc >= 18.5 && imc < 24.9) {
return el.innerHTML = nome + ' o seu IMC é '+ imc +' e Seu peso esta normal.';
} else if (imc >= 25 && imc < 29.9) {
return el.innerHTML = nome + ' o seu IMC é '+ imc +' Você esta com sobrepeso.';
} else {
return el.innerHTML = nome + ' o seu IMC é '+ imc +' e Você esta muito acima do peso.';
}
}
avaliarIMC(imc);
};
function toPlayAlert(element, Balloon, expression) {
const input = document.querySelector(element);
const inputErro = document.querySelector(Balloon);
const regexArray = [/^(\d.\.?)$/, /^[A-Za-z]+$/, /<("[^"]*"|'[^']*'|[^'">])*>/];
const expressionValue = new RegExp(expression);
let exp;
regexArray.map((el) => {
if (el.toString() === expressionValue.toString()) {
exp = el;
return exp;
}
})
if (input.type === "text") {
input.addEventListener('keydown', function (e) {
if (e.key === '.' || e.key === ';' || e.key === ',') {
e.preventDefault();
}
});
input.addEventListener('input', function () {
let valor = input.value;
if (valor === "") {
inputErro.style.display = 'none';
}
else if (valor.match(exp)) {
inputErro.style.display = 'none';
} else {
inputErro.style.display = 'block';
}
});
console.log("O input é do tipo nome.");
}
else if (input.type === "number") {
input.addEventListener('keydown', function (e) {
if (e.key === 'e' || e.key === 'E' || e.key === ',') {
e.preventDefault();
}
});
input.addEventListener('input', (evt) => {
let valor = input.value;
if (exp == null && Balloon == null) {
//get out
}
else {
if (exp.test(valor) || !valor === "") {
input.value = valor.substring(0, valor.length - 1);
inputErro.style.display = 'block';
console.log('block');
} else {
console.log('none');
inputErro.style.display = 'none';
}
}
});
}
};
toPlayAlert("#altura", "#balaoErro-2", /^(\d.\.?)$/);
toPlayAlert("#nome", "#balaoErro-1", /^[A-Za-z]+$/);
toPlayAlert("#peso");
btn.addEventListener('click', () => {
const nome = document.getElementById('nome');
const altura = document.getElementById('altura').value;
const peso = document.getElementById('peso').value;
calcular_imc(peso, altura,nome,'.text-dark');
})