A crazy calculation quesiton?!?

# Thread: A crazy calculation quesiton?!?

1. Junior Member
Join Date
Dec 1969
Posts
12

## A crazy calculation quesiton?!?

I&#039;m trying to perform a very simple calculation inside my form tags. In addition to my other textfields, select boxes, etc., I have 3 additional text fields: Start time, end time, and result. I&#039;m trying to get the end time field to subtract the start time field to give me a result. <BR><BR>::HERE&#039;S THE TOUGH PART:: <BR><BR>The result needs to be challenged:: <BR>If &#062;8 then result=1 <BR>If &#062;8 and &#060;23 then result=2 <BR>If &#062;23 and &#060;58 then result=3 <BR>If &#062;58 and &#060;63 then result=4 <BR>If &#062;63 then resul=5 <BR><BR>and for the kick in the pants: the result needs to be performed before the form is submitted. <BR><BR>Am I out of my mind? Do I need to pop-up a simple calculator? I&#039;m trying to idiot proof the text field... <BR><BR>Thanks for your help.

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Not sure how advanced this is...

...but what the heck.<BR><BR>But you left out an important point. You say you want to subtract the start time from the end time and then base the result on a range of numbers. Okay. Fine. And what are the *units* of the numbers? Milliseconds? Weeks? Centuries? Millenia?<BR><BR>Also, the way you have specified the condition, if the difference is exactly 8, 23, 58, or 63, then you will have no result at all. Is that what you want??? (I doubt it.)<BR><BR>p.s.: And what does "the result needs to be [hl="lightyellow"]challenged[/hl]" mean?<BR><BR>And... Why does this need to be don before being submitted? Because you have no control over the page it is submitted to? I presume, then, you mean that you want to use client-side JavaScript to code this?<BR><BR>Anyway, this is really pretty dirt easy, once you decide what the units of difference are supposed to be. And what to do with 8, 23, 58, and 63.<BR><BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
484

## highligthed that is...

[hl="lightyellow"]challenged[/hl]<BR><BR>interesting little trick. I did not know it is out there...<BR><BR>Now [hl="red"]HOW[/hl] do you do that ?

4. Senior Member
Join Date
Dec 1969
Posts
484

## Well now I know

.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## For those interested...

...it&#039;s done this way:<BR><BR>http://www.aspmessageboard.com/forum/formatting.asp<BR><BR>

6. Junior Member
Join Date
Dec 1969
Posts
12

## RE: Not sure how advanced this is...

All units are based on a 24 hour clock. The result should be in minutes.<BR><BR>[end time] - [start time] = [x]minutes<BR>if x &#060;=8 then write the whole number "1" <BR>if x &#062;8 and &#060;=23 then write the whole number "2"<BR>and so on...<BR><BR>As for doing the calculation prior to form submission is:<BR>The calculation resides inside of a much broader form that contains other important data. I need the result prior to form submission in order to:<BR><BR>1) send it to the correct place for database storage<BR>2) available for visual verification as the result determines future input in other text fields in the form.<BR><BR>FYI: I use the word challenge as a math term. e.g.&#062; the result must be challenged against a pre-defined list of variables in order to obtain it&#039;s value. If that isn&#039;t a normal term in asp, then I apologize for the confusion. :)<BR><BR>Thanks for your help...I truly hope it&#039;s dirt easy. I have searched the web over and over for more than a week...and this is where I ended up.<BR><BR>--Greg

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ahhh...a proper challenge...

Heh! Shame on me. And I&#039;m a math major, too. (I won&#039;t tell you how many years ago I graduated. I will tell you that I&#039;ve used *one* of my math courses in all my years of programming. And that even that use is long obsolete, now.)<BR><BR>You say "all units are based on a 24 hour clock". But how is the info entered into the form? 1723? or 17:23? I will assume 17:23 (we just take the user&#039;s input string and remove the colon, via the replace() function...we remove spaces, just in case, at the same time).<BR><BR>Okay, so something like this:<BR><BR>&#060;FORM Name="TheForm" ...&#062;<BR>&#060;INPUT Name="StartTime"&#062;<BR>&#060;INPUT Name="EndTime"&#062;<BR>&#060;INPUT Name="Result" onFocus="this.form.EndTime.focus();"&#062;<BR>&#06 0;INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"&#062;<BR>...<BR>&# 060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>function getResult(frm)<BR>{<BR> // get raw text as entered by user<BR> startAt = frm.StartTime.value;<BR> endAt = frm.EndTime.value;<BR> // remove colon and any white space in user response<BR> startAt = startAt.replace(/:/,"").replace(/s/g,"");<BR> endAt = endAt.replace(/:/,"").replace(/s/g,"");<BR> // convert to integers<BR> startAt = parseInt( startAt );<BR> endAt = parseInt( endAt );<BR> // *are* they integers?<BR> if ( isNaN(startAt) &#124&#124 isNaN(endAt) )<BR> { <BR> alert("start or end time is not a valid time");<BR> return false;<BR> }<BR> // except now treat them as *times*:<BR> startHour = Math.floor( startAt / 100 );<BR> endHour = Math.floor( endAt / 100 );<BR> startMin = startAt - 100 * startHour;<BR> endMin = endAt - 100 * endHour;<BR> if ( startHour &#062; 23 &#124&#124 endHour &#062; 23 &#124&#124 startMin &#062; 59 &#124&#124 endMin &#062; 59 )<BR> {<BR> alert("start or end time is not a valid time");<BR> return false;<BR> }<BR> // okay...valid times...convert to minutes:<BR> startAt = startHour * 60 + startMin;<BR> endAt = endHour * 60 + endMin;<BR> // if endAt less than startAt, assume it is next day...<BR> if ( endAt &#060; startAt ) endAt += (24 * 60); // add 24 hours<BR> // finally, the difference!<BR> diffMin = endAt - startAt;<BR><BR> // now get the result value...<BR> var result;<BR> if ( diffMin &#060;= 8 ) result = 1;<BR> else if ( diffMin &#060;= 23 ) result = 2;<BR> else if ( diffMin &#060;= 58 ) result = 3;<BR> else if ( diffMin &#060;= 63 ) result = 4;<BR> else result = 5;<BR><BR> // make it visible:<BR> frm.Result.value = result;<BR> // done<BR> return true;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>********************<BR><BR>10 0% *UN*tested, off the top of my head.<BR><BR>If it doesn&#039;t work, put a bunch of alert(...) calls into it to see where it is getting to and what is and isn&#039;t working and then ask more.<BR><BR>Since you didn&#039;t say what to do with it, I put the result number back into a visible field in the form. I made that field read-only by the simple expediency of moving the cursor off of it whenever it arrives there. Your mileage may vary.<BR><BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## That was pretty dumb of me...

Sigh. Why did I do the same calcs twice, instead of creating a function to do them? I dunno. Becuz it&#039;s late out?<BR><BR>&#060;FORM Name="TheForm" ...&#062;<BR>&#060;INPUT Name="StartTime"&#062;<BR>&#060;INPUT Name="EndTime"&#062;<BR>&#060;INPUT Name="Result" onFocus="this.form.EndTime.focus();"&#062;<BR>&#06 0;INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"&#062;<BR>...<BR>&# 060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>function convertTime(fld)<BR>{<BR> var txtval = fld.value;<BR> // remove colon and any white space in user response<BR> var rawnum = txtval.replace(/:/,"").replace(/s/g,"");<BR> // convert to integer<BR> var intnum = parseInt( rawnum );<BR> // *is* it an integer?<BR> if ( isNaN(intnum) )<BR> { <BR> alert( fld.name + " is not a valid time");<BR> return -1;<BR> }<BR> // except now treat it as a time<BR> hr = Math.floor( intnum / 100 );<BR> mn = intnum - 100 * hr;<BR> if ( hr &#062; 23 &#124&#124 mn &#062; 59 )<BR> {<BR> alert( fld.name + " is not a valid time");<BR> return -1;<BR> }<BR> return hr * 60 + mn; // time, in minutes<BR>}<BR>function getResult(frm)<BR>{<BR> startAt = convertTime( frm.StartTime );<BR> endAt = convertTime( frm.EndTime );<BR> if ( startAt &#060; 0 &#124&#124 endAt &#060; 0 ) return false;<BR><BR> // okay...valid times...<BR> // if endAt less than startAt, assume it is next day...<BR> if ( endAt &#060; startAt ) endAt += (24 * 60); // add 24 hours<BR> // finally, the difference!<BR> diffMin = endAt - startAt;<BR><BR> // now get the result value...<BR> var result;<BR> if ( diffMin &#060;= 8 ) result = 1;<BR> else if ( diffMin &#060;= 23 ) result = 2;<BR> else if ( diffMin &#060;= 58 ) result = 3;<BR> else if ( diffMin &#060;= 63 ) result = 4;<BR> else result = 5;<BR><BR> // make it visible:<BR> frm.Result.value = result;<BR> // done<BR> return true;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>********************<BR><BR>St ill 100% *UN*tested, but more logically structured.<BR><BR>

9. Junior Member
Join Date
Dec 1969
Posts
12

## RE: That was pretty dumb of me...

You are mad man. The calculation works perfectly...Thanks for the input. The script I was attempting danced all around it, but never on it. Of course I am a novice self-learner...still room to grow. <BR><BR>I also had the need for the total minutes for another textfield. <BR><BR>so I created:<BR><BR>var = minutecount<BR>minutecount = (endAt - startAt)<BR><BR>frm.minutecount.value = minutecount<BR><BR>called the function on the same submit command and wrote it to the specified input field....and all is well :)<BR><BR>The sad part is that I have 4 different start and end time fields to deal with. This function only calls for 1. I guess I&#039;ll need to create 3 more functions...that&#039;s a lot of script for a page huh?<BR><BR>Thanks again.<BR>--Greg

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## NO NO NO!!!

Never write 4 functions to do the same thing!<BR><BR>That&#039;s the whole *POINT* of using functions!<BR><BR>Just change this:<BR><BR>function getResult(frm) <BR>{ <BR> startAt = convertTime( frm.StartTime ); <BR> endAt = convertTime( frm.EndTime ); <BR> ...<BR> frm.Result.value = result; <BR><BR>}<BR><BR>To this:<BR><BR>function getResult(fldStart, fldEnd, fldResult) <BR>{ <BR> startAt = convertTime( fldStart ); <BR> endAt = convertTime( fldEnd ); <BR> ...<BR> fldResult.value = result; <BR>}<BR><BR>And then instead of this:<BR><BR>&#060;INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"&#062; <BR><BR>Something like this:<BR><BR>&#060;INPUT Type=Button Value="click to get result 1"<BR> onClick="getResult(this.form.start1,this.form.end1 ,this.form.result1);"&#062; <BR>&#060;INPUT Type=Button Value="click to get result 2" <BR> onClick="getResult(this.form.start2,this.form.end2 ,this.form.result2);"&#062; <BR>... <BR><BR>Or, if you need to do them all from one button:<BR><BR>&#060;INPUT Type=Button Value="click to get ALL results" onClick="getAllResults(this.form);"&#062; <BR><BR>&#060;SCRIPT&#062;<BR>function getAllResults(frm)<BR>{<BR> getResults(frm.start1,frm.end1,frm.result1);<BR> getResults(frm.start2,frm.end2,frm.result2);<BR> getResults(frm.start3,frm.end3,frm.result3);<BR> getResults(frm.start4,frm.end4,frm.result4);<BR>}< BR>&#060;/SCRIPT&#062;<BR><BR>Learn to *USE* functions!<BR><BR>

#### Posting Permissions

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