1- document . addEventListener ( 'submit' , function ( evento ) {
1+ /*
2+ Formulário envio de dados para cálculo da Média
3+ */
24
3- evento . preventDefault ( ) ; //Formulário para de ter o comportamento padrão do html
4- evento . stopPropagation ( ) ; //Faz com que todo submit de form caia nessa função
5+ document . getElementById ( "form-01" ) . addEventListener ( "submit" , function ( evento ) {
6+ evento . preventDefault ( ) ; //Formulário para de ter o comportamento padrão do html
7+ evento . stopPropagation ( ) ; //Faz com que todo submit de form caia nessa função
58
6- let form = document . getElementById ( 'form-01' ) ;
9+ if ( this . getAttribute ( "class" ) . match ( / e r r o / ) ) {
10+ return false ;
11+ }
712
8- let dados = new FormData ( form )
13+ let dados = new FormData ( this ) ;
914
10- let objeto = { } ; //Assim como array, string, número também é um tipo de variável
15+ let notas = [ ] ;
1116
12- let notas = [ ] ;
17+ for ( let key of dados . keys ( ) ) {
18+ let numero = dados . get ( key ) . match ( / \d * / ) ? Number ( dados . get ( key ) ) : 0 ; // ParseFloat - Número com ponto depois do decimal
19+ // .match(/\d/) verifica se contem um digito se sim transforma em um número se não transforma em 0
20+ console . log ( typeof numero ) ;
1321
14- for ( let key of dados . keys ( ) ) {
15- objeto [ key ] = dados . get ( key ) ;
22+ if ( ! isNaN ( numero ) ) {
23+ notas . push ( numero ) ;
24+ }
25+ }
26+
27+ console . log ( notas ) ;
28+
29+ function calcularMedia ( notas ) {
30+ var soma = 0 ;
31+ for ( c = 0 ; c < notas . length ; c ++ ) {
32+ soma += notas [ c ] ;
33+ }
34+
35+ media = soma / notas . length ; //Utilizar notas.length para que ele calcule dinamicamente a quantidade que ela recebeu
1636
17- //Adicionar itens o array
18- notas . push ( parseInt ( dados . get ( key ) ) ) ;
19- }
37+ return media ;
38+ }
2039
21- console . log ( notas ) ;
22- console . log ( objeto ) ;
40+ function aprovacao ( notas ) {
41+ let media = calcularMedia ( notas ) ;
2342
24- function calcularMedia ( notas ) {
43+ if ( media >= 7 ) {
44+ condicao = "Aprovado" ;
45+ document . getElementById ( "resultado" ) . style . backgroundColor = "Green" ;
46+ } else if ( media >= 4 ) {
47+ condicao = "Recuperação" ;
48+ document . getElementById ( "resultado" ) . style . backgroundColor = "Yellow" ;
49+ } else {
50+ condicao = "Reprovado" ;
51+ document . getElementById ( "resultado" ) . style . backgroundColor = "Red" ;
52+ }
53+ return "Média: " + media + " - Resultado: " + condicao ;
54+ }
55+
56+ document . getElementById ( "resultado" ) . innerHTML = aprovacao ( notas ) ;
57+ aprovacao ( notas ) ;
58+ } ) ;
2559
26- var soma = 0
27- for ( c = 0 ; c < notas . length ; c ++ ) {
28- soma += notas [ c ]
29- }
30-
31- media = soma / notas . length //Utilizar notas.length para que ele calcule dinamicamente a quantidade que ela recebeu
32-
33- return media ;
34- }
60+ function Validation ( elemento ) {
61+ elemento . addEventListener ( "focusout" , function ( event ) {
62+ event . preventDefault ( ) ;
3563
36- function aprovacao ( notas ) {
37- let media = calcularMedia ( notas ) ; // escopo da função
38- let condicao = media >= 7 ? "aprovado" : "reprovado" ;
39- return 'Média: ' + media + ' - Resultado: ' + condicao ;
64+ if ( this . value == "" ) {
65+ document . querySelector ( ".mensagem" ) . innerHTML = "Verifique o preenchimento dos campos em vermelho" ;
66+ this . classList . add ( "erro" ) ;
67+ this . parentNode . classList . add ( "erro" ) ; //Adiciona Classe no ParerntNode(Pai) no caso no formulário, impedindo que envie o formulário
68+ return false ;
69+ } else {
70+ document . querySelector ( ".mensagem" ) . innerHTML = "" ;
71+ this . classList . remove ( "erro" ) ;
72+ this . parentNode . classList . remove ( "erro" ) ;
4073 }
74+ } ) ;
75+ }
76+
77+ function NumericValidation ( elemento ) {
78+ elemento . addEventListener ( "focusout" , function ( event ) {
79+ event . preventDefault ( ) ;
80+ if ( this . value != "" && this . value . match ( / [ 0 - 9 ] * / ) && this . value >= 0 && this . value <= 10 ) {
81+ document . querySelector ( ".mensagem" ) . innerHTML = "" ;
82+ this . classList . remove ( "erro" ) ;
83+ this . parentNode . classList . remove ( "erro" ) ;
84+ } else {
85+ document . querySelector ( ".mensagem" ) . innerHTML = "O campo é apenas numérico" ;
86+ this . classList . add ( "erro" ) ;
87+ this . parentNode . classList . add ( "erro" ) ; //Adiciona Classe no ParerntNode(Pai) no caso no formulário, impedindo que envie o formulário
88+ return false ;
89+ }
90+ } ) ;
91+ }
92+
93+ let Necessary = document . querySelectorAll ( "input.obrigatorio" ) ;
94+ let Numeric = document . querySelectorAll ( "input.numero" ) ;
4195
42- document . getElementById ( 'resultado' ) . innerHTML = aprovacao ( notas ) ;
43- aprovacao ( notas ) ;
96+ for ( let Focused of Necessary ) {
97+ Validation ( Focused ) ;
98+ }
4499
45- } ) ;
100+ for ( let Focused of Numeric ) {
101+ NumericValidation ( Focused ) ;
102+ }
0 commit comments