Validation with Java Script

Asked

Viewed 83 times

1

Expensive;

I created a function in Java Script, where this function is intended to pick the current date and upon this date, as the user selects the flag of his card, calculate whether the invoice will be inserted in the table of the current month or next month, according to its due date and closing date.

Follows function:

 <script>
function finance() {

var dtcompra = document.getElementById("payment").value;
var datacompra = new Date(dtcompra);
var diacompra = datacompra.getDate();
var mespgto = document.getElementById("mespgto");
var itemSelecionado = mespgto.options[mespgto.selectedIndex].value;

var diamanualnubank = 25; // Vencimento Nubank
var diamanualamex = 15; // Vencimento Amex
var diamanualsantanderplatinum = 17; // Vencimento Santander Plantinum

// Função para retornar a data Atual
var data = new Date();
var dia = data.getDate();
   if (dia.toString().length == 1)
      dia = "0"+dia;
var mes = data.getMonth()+1
    if (mes.toString().length == 1)
    mes = "0"+mes;
var ano = data.getFullYear();  
//var data = ano+"/"+mes+"/"+dia;


if (itemSelecionado == 'nubank' && diacompra < 18) {


document.getElementById('maturity').value = ano+"/"+(data.getMonth()+1)+"/"+diamanualnubank;

    } else {
    document.getElementById('maturity').value = ano+"/"+(data.getMonth()+2)+"/"+diamanualnubank;

  return false;
}

if (itemSelecionado == 'amex' && diacompra > 10 &&  diacompra < 30) 

document.getElementById('maturity').value = ano+"/"+(data.getMonth()+1)+"/"+diamanualamex;

    else  {
    document.getElementById('maturity').value = ano+"/"+(data.getMonth()+2)+"/"+diamanualamex;

        return false;
}


if (itemSelecionado == 'santanderplatinum' && diacompra < 10) 

document.getElementById('maturity').value = ano+"/"+(data.getMonth()+1)+"/"+diamanualsantanderplatinum;

    else  {
    document.getElementById('maturity').value = ano+"/"+(data.getMonth()+2)+"/"+diamanualsantanderplatinum;

        return false;
}


}
</script>

If the current date is 09/10/2016 for ex and select Amex, should fill in the maturity input for 2016/09/15, if the current date is greater than 10, the completion of this input should be 2016/10/15. Simulating here, the completion is being with the expiration date of Nubank 2016/10/25. I’m not finding where I’m going wrong in logic.I’m not very good with Java Script and so I turn to you guys.

I count on your help. Thank you.

  • When running this code with the developer console open, what error do you get?

  • Olá Ifarroco, do not get errors, I believe it is some logic problem even. It runs, however with the wrong dates.

  • If possible create an https://jsfiddle.net with the html part, as its code references these elements. Also, add some "console.log()" along your logic. For example: before the if’s, put console.log('the selected item is', itemSelected', 'and the purchase day is ',diacompra'). So you can know which parameters are being used.

  • It is unclear what problem you are having... is this what you want? https://jsfiddle.net/0ffd56kz/

  • Hello Sergio, yes, with your code solved my problem. Very grateful . Ps: I tried to understand your logic for my learning and could not. When I have a moment and I can, I’d be able to comment on what he does to make me understand. I’m not very good at programming and would be interested to understand your logic. I saw that you created an array in the variable (dataPagamento), but you did not use the variable and pointed the array directly in the field value. He had read enough that in an IF java script was used within a "parentheses" block, but in each of his IF, two parentheses were used. Grateful.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.