Jump to content

problem with adding values and multiplication


bm4499

Recommended Posts

Does anyone know how to modify this code so that it deny in case a text field is empty? Next to that I want to multiply it by 1.5. Anyone?

onchange="document.getElementById('total').value = parseInt(document.getElementById('f1').value) + parseInt(document.getElementById('f2').value) + parseInt(document.getElementById('f3').value) + parseInt(document.getElementById('f4').value);"

 

thanks a lot in advance!

In your case, I'll make a function that will check the value and return the integer or a 0;

 

e.g.

function my_parse_int(val){
    val = parseInt(val, 10);
    if (isNaN(val)) return 0;
    return val;
};

... change the onchange event to
onchange="document.getElementById('total').value = (my_parse_int(document.getElementById('f1').value) ....) *1.5;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.