Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    total of fields and an alert.window

    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?

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    you could try

    onKeyUp="doCalc()"

    then the code could be



    Code:
    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:

    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...

  • #3
    New Coder
    Join Date
    Feb 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    I've adjusted your code a little bit:

    * activate the function:
    Code:
    <input type="text" name="b7x1_x1" size="1" onKeyUp="personen()">
    * the function:
    Code:
    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:
    Code:
    <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:
    Code:
    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?

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    you could do something like

    Code:
    	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"....

  • #5
    New Coder
    Join Date
    Feb 2004
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!
    It works!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •