calculate total & check for the integer (whole) -

# Thread: calculate total & check for the integer (whole) -

1. meeta Guest

## calculate total & check for the integer (whole) -

I need to check if data entered is numeric & it should not be 1.5 that is whole number. Then autocalculate total in the last field.<BR>I&#039;ve Bill(to check for integer, not sure how to check if number is whole) & Blake&#039;s(to calculate total) code, I don&#039;t know how to make it work with my code. Also code below which displayes categories is in ASP page with while loop it&#039;ll be repeated till some records found.<BR><BR>&#060;script language="JavaScript"&#062;<BR>&#060;!--<BR>function numval(theField)<BR>{<BR>if ( isNaN( parseInt(theField.value ) ) ) <BR>{ <BR> alert("You didn&#039;t put an integer"); <BR> theField.focus()<BR> theField.select()<BR>} <BR>}<BR><BR>function total?????<BR>//--&#062;&#060;/script&#062;<BR><BR> &#060;td valign=top&#062;&#060;input type=text name=cat1 size=3 maxlength=3 value=0 onBlur="numval(cat1)"&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;input type=text name=cat2 size=3 maxlength=3 value=0 onBlur="numval(cat2)"&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;input type=text name=cat3 size=3 maxlength=3 value=0 onBlur="numval(cat3)"&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;input type=text name=cat4 size=3 maxlength=3 value=0 onBlur="numval(cat4)"&#062;&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;input type=text size=3 name=total value=0&#062;&#060;/font&#062;&#060;/td&#062;<BR><BR>Thanks a bunch guys!

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

## One change, one mistake...

You do this:<BR><BR>&#060;td valign=top&#062;&#060;input type=text name=cat1 size=3 maxlength=3 value=0 onBlur="numval(cat1)"&#062;&#060;/td&#062; <BR><BR>That will *NOT* work. cat1, used as the argument to numval, will *not* be the current field object. It will just be some arbitrary global JavaScript variable, so it will always be zero.<BR><BR>Just change from<BR>&nbsp; &nbsp; onBlur="numval(cat1)"<BR>to<BR>&nbsp; &nbsp; onBlur="numval(this)"<BR><BR>The word "this" is a keyword in JavaScript that means "the current object"...in this case, the current input text field.<BR><BR>********************<BR><BR>And the minor change to your numval code:<BR><BR><BR>&#060;script language="JavaScript"&#062; <BR>&#060;!-- <BR>function numval(theField) <BR>{ <BR>&nbsp; &nbsp; var val = parseFloat(theField.value);<BR>&nbsp; &nbsp; if ( isNaN(val) &#124&#124 ( val != parseInt(val) ) )<BR>&nbsp; &nbsp; { <BR>&nbsp; &nbsp; &nbsp; &nbsp; alert("You didn&#039;t put an integer"); <BR>&nbsp; &nbsp; &nbsp; &nbsp; theField.value = "0";<BR>&nbsp; &nbsp; &nbsp; &nbsp; theField.focus(); <BR>&nbsp; &nbsp; &nbsp; &nbsp; // theField.select() ??? what is this??<BR>&nbsp; &nbsp; } <BR>} <BR><BR>**************<BR><BR>Oops...just noticed...you also wanted to get a total!<BR><BR>Hmmm...okay...<BR><BR>function getTotal( )<BR>{<BR>&nbsp; &nbsp; frm = document.FormName;<BR>&nbsp; &nbsp; total = 0;<BR>&nbsp; &nbsp; for ( var i = 1; i &#060;= 4; ++i )<BR>&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; total = total + parseInt( frm.elements["cat"+i].value );<BR>&nbsp; &nbsp; }<BR>&nbsp; &nbsp; frm.total.value = total;<BR>}<BR><BR>Then you just call getTotal() any time you want to update the total. For example, you might call it as the last line of your numval( ) function.<BR><BR>

3. meeta Guest

## RE: One change, one mistake...

Thanks. How can I call this function in total input box so that when one clicks in it, it shows total of all categories ?

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

## Well...

As the code was written, if you called it where I indicated, it would be updated every time anybody changed one of the individual values, so why would you *need* to click on it???<BR><BR>But if you want to:<BR><BR>&#060;INPUT Name="total" ... onFocus="getTotal();this.blur();"&#062;<BR><BR>See ? No click needed. Just trying to put the cursor into the field would do it.<BR><BR>

5. meeta Guest

## RE:it works!

thanks. It works! I think I need to go over Jscript tutorial, instead of bugging you!!<BR>I&#039;m pasting your code in case someone else needs it. <BR><BR>&#060;html&#062;<BR>&#060;head&#062;<BR> &#060;title&#062;Untitled&#060;/title&#062;<BR>&#060;script language="JavaScript"&#062; <BR>&#060;!-- <BR>function numval(theField) <BR>{ <BR> var val = parseFloat(theField.value); <BR> if ( isNaN(val) &#124&#124 ( val != parseInt(val) ) ) <BR> { <BR> alert("You didn&#039;t put an integer"); <BR> theField.value = "0"; <BR> theField.focus(); <BR> } <BR> <BR>} <BR><BR>function getTotal( ) <BR>{ <BR> frm = document.FormName; <BR> total = 0; <BR> for ( var i = 1; i &#060;= 4; ++i ) <BR> { <BR> total = total + parseInt( formname.elements["cat"+i].value ); <BR> } <BR> formname.total.value = total; <BR>} <BR>--&#062;<BR>&#060;/script&#062; <BR>&#060;/head&#062;<BR><BR>&#060;body&#062;<BR>&#060;form name=formname&#062;<BR>&#060;td valign=top&#062;&#060;input type=text name=cat1 size=3 maxlength=3 value=0 onBlur="numval(this);"&#062;&#060;/td&#062; <BR>&#060;td valign=top&#062;&#060;input type=text name=cat2 size=3 maxlength=3 value=0 onBlur="numval(this);"&#062;&#060;/td&#062; <BR>&#060;td valign=top&#062;&#060;input type=text name=cat3 size=3 maxlength=3 value=0 onBlur="numval(this);"&#062;&#060;/td&#062; <BR>&#060;td valign=top&#062;&#060;input type=text name=cat4 size=3 maxlength=3 value=0 onBlur="numval(this);"&#062;&#060;/td&#062; <BR>&#060;td valign=top&#062;&#060;input type=text size=3 name=total value=0 onFocus="getTotal();this.blur();"&#062;&#060;/font&#062;&#060;/td&#062; <BR><BR>&#060;/form&#062;<BR>

6. meeta Guest

## RE: in my aspcode total is not showing..

here is my asp code. I did change total function to get value from 7 categories.<BR><BR>&#060;form name=formname action="update.asp" method=post&#062;<BR>&#060;%<BR>set recordsset....<BR>while not rsemp.eof<BR> response.write "&#060;tr&#062;&#060;td valign=top&#062;&#060;font face=arial size=2&#062;" & rsemp("Lname") & " " & rsemp("fname") &"&#060;/font&#062;&#060;/td&#062;" & _<BR> "&#060;td valign=top&#062;&#060;font face=arial size=2&#062;" & rsemp("InstructorLname") & "&#060;/font&#062;&#060;/td&#062;" & _<BR> %&#062;<BR> <BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat1 size=3 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat2 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat3 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat4 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat5 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat6 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=cat7 size=3 maxlength=4 value=0 onBlur="numval(this);"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;td valign=top&#062;&#060;font face=arial size=2&#062;&#060;input type=text name=total size=5 value=0 onFocus="getTotal();this.blur();"&#062;&#060;/font&#062;&#060;/td&#062;<BR> &#060;/tr&#062; <BR>&#060;%<BR> rsemp.movenext<BR> wend<BR> response.write "&#060;/table&#062;"<BR> %&#062;<BR><BR>&#060;input type=submit value="Submit Report"&#062;<BR>&#060;input type=reset value="Clear Fields"&#062;<BR>&#060;/form&#062;<BR><BR>Thanks<BR>Meeta

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

## Why should it???

You never try to show the total in your *ASP* code (to make the distinction from the JS code in the browser).<BR><BR>I don&#039;t see any place where you code Request.Form("total")<BR><BR>Incidentally, how many of these rsemp(...) do you expect on a single page? You do "while not rsemp.eof" so I presume several? If so, then how will you keep separate (on the next page) the values for each emp(loyee)???? They will all get lumped together in comma-delimited strings.<BR><BR>

8. meeta Guest

## RE: using split ...

on the next page i&#039;ve this code. Its updating records.<BR>i&#039;m passing userid, fname & lname as hidden so it would be like<BR><BR>first id<BR>first fname<BR>first lanme<BR>fist cat<BR>so on & so forth<BR><BR>strcat1 = request.form("cat1")<BR>strcat2 = request.form("cat2")<BR>arrcat1 = split(strcat1, ", ", -1, 1)<BR>arrcat2 = split(strcat2, ", ", -1, 1)<BR><BR> set rsr=Conn.execute("select * from webentry where employer=&#039;"& empid &"&#039;") <BR> while not rsr.eof<BR> rfname = rsr("fname")<BR> rlname = rsr("lname")<BR> for i = lbound(arrcat1) to ubound(arrcat1)<BR> fname = arrfname(i)<BR> lname = arrlname(i)<BR> if rfname = fname then<BR> sqm = "update resojtwebentry set category1="& arrcat1(i) &",category2="& arrcat2(i) &",category3="& arrcat3(i) &",category4="& arrcat4(i) &",category5="& arrcat5(i) &",category6="& arrcat6(i) &",[total month&#039;s ojt]="& arrtotal(i) &" where lname =&#039;" & lname & "&#039;"<BR> set rs1=conn.execute(sqm)<BR> end if<BR> next<BR> rsr.movenext<BR> wend

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

## RE: using split ...

Uggghhh...oh, well... if it works.<BR><BR>But now I don&#039;t see how the JS code on the prior page works, at all! Since if you have multple &#060;INPUT Name="cat1"&#062; fields, then you need to use arrays in the JS code instead of individual items.<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
•