SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validation of a timesheet

    Hi guru's

    First of all, my english is quite poor, so please feel free to ask some questions, if my explanation is not good enough...

    I have a timesheet form, with 4 "kinds" of fields:

    fee
    hour[] (31 of theese)
    total_hours
    total_fee

    As you can see, i have an array of one field, and some single ones, I would like to have function that validates all of this, when there is an onchange on either the fee or hour[] field, it should validate, and calculate the total_hours, and the total_fee (fee * total_hours).
    But since it would be meaningless to calculate total_fee, if either fee or total_hours is not set, it should validate to check if they're set, and that it is plain numbers.

    Hope you understand me

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you're looking for something like this:
    Code:
    <html>
    <head>
    <script language="JavaScript">
    function Validate(form){
    	var nFee = Number(form.fee.value);
    	if( nFee == 0 ) {
    		form.totalhours.value = "";
    		form.totalfee.value = "";
    	} else {
    		var nTotHours = 0;
    		for( var i=0; i < form.hours.length; i++ ) {
    			if( form.hours[i].value.length > 0 ) {
    				nTotHours += Number(form.hours[i].value);
    			}
    		}
    		if( nTotHours > 0 ) {
    			form.totalhours.value = nTotHours;
    			form.totalfee.value = nFee * nTotHours;
    		} else {
    			form.totalhours.value = "";
    			form.totalfee.value = "";
    		}
    	}
    }
    </script>
    </head>
    <body>
    <form id="theform">
    Fee:<input type="text" name="fee"><br>
    <br>
    Hours:<input type="text" name="hours"><br>
    Hours:<input type="text" name="hours"><br>
    Hours:<input type="text" name="hours"><br>
    Hours:<input type="text" name="hours"><br>
    Hours:<input type="text" name="hours"><br>
    Hours:<input type="text" name="hours"><br>
    (etc)<br>
    <br>
    TotalHours:<input type="text" name="totalhours"><br>
    TotalFee:<input type="text" name="totalfee"><br>
    <input type="button" value="Validate" onclick="Validate(document.getElementById('theform'));"> 
    </form>
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for the reply, but i got a solution elsewhere.

    This is the solution i got, and it works perfectly:

    Code:
    <script type="text/JavaScript">
    function calc(f) {
        var tot, elms, val = parseInt(f.fee.value);
        if (val=="" || !(val-1<val)) {
            f.total_fee.value = 0;
            return;
        }
        tot = 0;
        elms = f.elements["hour[]"];
        for (i=0,j=elms.length; i<j; i++) {
            val = parseInt(elms[i].value);
            if (val!="" && val-1<val) tot += val;
        }
        f.total_hour.value = tot;
        f.total_fee.value = tot * parseInt(f.fee.value);
    }
    </script>
    
    <form>
    Fee: <input type="text" name="fee" onblur="calc(this.form)"><br>
    Hour: <input type="text" name="hour[]" onblur="calc(this.form)"><br>
    Hour: <input type="text" name="hour[]" onblur="calc(this.form)"><br>
    Hour: <input type="text" name="hour[]" onblur="calc(this.form)"><br>
    <hr>
    Total Hours: <input type="text" name="total_hour" onblur="calc(this.form)"><br>
    Total Fee: <input type="text" name="total_fee" onblur="calc(this.form)"><br>
    </form>


Bookmarks

Posting Permissions

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