...

View Full Version : total of fields and an alert.window



Drieske1982
03-12-2004, 10:17 AM
Hi,
Still working on my form. :)
http://www.aendekerk.be/beta/airco/bereken.html

I have 3 fields "b7x1_x1", "b7x1_x2" and "b7x1_x3".
I would like to have a function that 'onkeyup' (onkeyup="some_function()") calculates the total of these three values and puts it in the field "b8x1_x1".

If a user try's to click, or fill out "b8x1_x1", there should be an alert.window that says something like "please fill out x instead of y...".

Is this possible?

homerUK
03-12-2004, 10:27 AM
you could try

onKeyUp="doCalc()"

then the code could be





function doCalc()
{
//set the values
var b7x1_x1 = document.formname.b7x1_x1.value;
var b7x1_x2 = document.formname.b7x1_x2.value;
var b7x1_x3 = document.formname.b7x1_x3.value;
var b8x1_x1 = document.formname.b8x1_x1.value;

//do the calc
total = (parseFloat(b7x1_x1) + parseFloat(b7x1_x2) + parseFloat(b7x1_x3));

//send back the result
b8x1_x1 = total;
}




then on the total field have something like

<input type="text" name="b8x1_x1" disabled onClick="noInput()">

then have this code:



function noInput()
{
alert("This field is disabled. Please fill in the other fields");
}


I've not tried the code, so let us know how it goes... :p

Drieske1982
03-12-2004, 11:55 AM
Hi,
I've adjusted your code a little bit:

* activate the function:

<input type="text" name="b7x1_x1" size="1" onKeyUp="personen()">* the function:
function personen()
{
d = document.aForm;
var b7x1_x1 = d.b7x1_x1.value;
var b7x2_x1 = d.b7x2_x1.value;
var b7x3_x1 = d.b7x3_x1.value;
var b8x1_x1 = d.b8x1_x1.value;
persoon_totaal = (parseFloat(b7x1_x1) + parseFloat(b7x2_x1) + parseFloat(b7x3_x1));
d.b8x1_x1.value = persoon_totaal;
}(The most important change is replacing b8x1_x1 by d.b8x1_x1.value)

* activate the second function:
<input type="text" name="b8x1_x1" size="1" readonly unselectable onClick="b8x1_x1_uit()">("readonly unselectable" instead of "disabled", otherwise it wouldn't work).
* the function:
function b8x1_x1_uit()
{
alert("Gelieve het aantal personen op te delen naar rookgewoonte en in te vullen in 7.");}

Now i've noticed that the first function only works when all the fields (b7x1_x1 , b7x2_x1 , b7x3_x1) are filled out. When only the first 2 (e.g.) are filled out, the value of b8x1_x1 is "NaN". Is it possible that this function interpretes empty fields as a zero?

homerUK
03-12-2004, 12:00 PM
you could do something like



d = document.aForm;
var b7x1_x1 = d.b7x1_x1.value;
var b7x2_x1 = d.b7x2_x1.value;
var b7x3_x1 = d.b7x3_x1.value;
var b8x1_x1 = d.b8x1_x1.value;

if ((b7x1_x1 == "NaN") || (b7x1_x1 == "") || (b7x1_x1 == "NULL")) { b7x1_x1 = "0"; }
if ((b7x2_x1 == "NaN") || (b7x2_x1 == "") || (b7x2_x1 == "NULL")) { b7x2_x1 = "0"; }
if ((b7x3_x1 == "NaN") || (b7x3_x1 == "") || (b7x3_x1 == "NULL")) { b7x3_x1 = "0"; }

persoon_totaal = (parseFloat(b7x1_x1) + parseFloat(b7x2_x1) + parseFloat(b7x3_x1));
d.b8x1_x1.value = persoon_totaal;


I'm sure there is probably a better way of coding it, but you need to say that if one of the fields is empty, then use a "0"....

Drieske1982
03-12-2004, 02:37 PM
Thanks!
It works!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum