
A crazy calculation quesiton?!?
I'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'm trying to get the end time field to subtract the start time field to give me a result. <BR><BR>::HERE'S THE TOUGH PART:: <BR><BR>The result needs to be challenged:: <BR>If >8 then result=1 <BR>If >8 and <23 then result=2 <BR>If >23 and <58 then result=3 <BR>If >58 and <63 then result=4 <BR>If >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 popup a simple calculator? I'm trying to idiot proof the text field... <BR><BR>Thanks for your help.

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

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 ?

Well now I know

For those interested...
...it's done this way:<BR><BR>http://www.aspmessageboard.com/forum/formatting.asp<BR><BR>

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 <=8 then write the whole number "1" <BR>if x >8 and <=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.> the result must be challenged against a predefined list of variables in order to obtain it's value. If that isn't a normal term in asp, then I apologize for the confusion. :)<BR><BR>Thanks for your help...I truly hope it'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

Ahhh...a proper challenge...
Heh! Shame on me. And I'm a math major, too. (I won't tell you how many years ago I graduated. I will tell you that I'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'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><FORM Name="TheForm" ...><BR><INPUT Name="StartTime"><BR><INPUT Name="EndTime"><BR><INPUT Name="Result" onFocus="this.form.EndTime.focus();"><BR> 0;INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"><BR>...<BR>&# 060;/FORM><BR><BR><SCRIPT><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)  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 > 23  endHour > 23  startMin > 59  endMin > 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 < 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 <= 8 ) result = 1;<BR> else if ( diffMin <= 23 ) result = 2;<BR> else if ( diffMin <= 58 ) result = 3;<BR> else if ( diffMin <= 63 ) result = 4;<BR> else result = 5;<BR><BR> // make it visible:<BR> frm.Result.value = result;<BR> // done<BR> return true;<BR>}<BR></SCRIPT><BR><BR>********************<BR><BR>10 0% *UN*tested, off the top of my head.<BR><BR>If it doesn't work, put a bunch of alert(...) calls into it to see where it is getting to and what is and isn't working and then ask more.<BR><BR>Since you didn't say what to do with it, I put the result number back into a visible field in the form. I made that field readonly by the simple expediency of moving the cursor off of it whenever it arrives there. Your mileage may vary.<BR><BR><BR>

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's late out?<BR><BR><FORM Name="TheForm" ...><BR><INPUT Name="StartTime"><BR><INPUT Name="EndTime"><BR><INPUT Name="Result" onFocus="this.form.EndTime.focus();"><BR> 0;INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"><BR>...<BR>&# 060;/FORM><BR><BR><SCRIPT><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 > 23  mn > 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 < 0  endAt < 0 ) return false;<BR><BR> // okay...valid times...<BR> // if endAt less than startAt, assume it is next day...<BR> if ( endAt < 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 <= 8 ) result = 1;<BR> else if ( diffMin <= 23 ) result = 2;<BR> else if ( diffMin <= 58 ) result = 3;<BR> else if ( diffMin <= 63 ) result = 4;<BR> else result = 5;<BR><BR> // make it visible:<BR> frm.Result.value = result;<BR> // done<BR> return true;<BR>}<BR></SCRIPT><BR><BR>********************<BR><BR>St ill 100% *UN*tested, but more logically structured.<BR><BR>

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 selflearner...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'll need to create 3 more functions...that's a lot of script for a page huh?<BR><BR>Thanks again.<BR>Greg

NO NO NO!!!
Never write 4 functions to do the same thing!<BR><BR>That'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><INPUT Type=Button Value="click to get result" onClick="getResult(this.form);"> <BR><BR>Something like this:<BR><BR><INPUT Type=Button Value="click to get result 1"<BR> onClick="getResult(this.form.start1,this.form.end1 ,this.form.result1);"> <BR><INPUT Type=Button Value="click to get result 2" <BR> onClick="getResult(this.form.start2,this.form.end2 ,this.form.result2);"> <BR>... <BR><BR>Or, if you need to do them all from one button:<BR><BR><INPUT Type=Button Value="click to get ALL results" onClick="getAllResults(this.form);"> <BR><BR><SCRIPT><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></SCRIPT><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

Forum Rules

