ASP Form Calculation

1. Member
Join Date
Dec 1969
Posts
47

## ASP Form Calculation

Hi, I&#039;ve created a form where the form field integer values are added to create and display a total once submitted, but my form is displaying a Mismatch error. Do anyone have any suggestions? Thanks for your help! Here&#039;s the code.<BR><BR>&#060;% dim field1, field2, field3<BR>field1 = request.form("field1") <BR>field2 = request.form("field2")<BR>field3 = request.form("field3") <BR>total = field1*1 + field2*1 + field3*1<BR>%&#062;<BR><BR>&#060;form action="untitled2.asp" method="post"&#062;<BR>Field 1:&nbsp;&nbsp;&#060;input type="text" name="field1"&#062;<BR><BR><BR>Field 2:&nbsp;&nbsp;&#060;input type="text" name="field2"&#062;<BR><BR><BR>Field 3:&nbsp;&nbsp;&#060;input type="text" name="field3"&#062;<BR><BR><BR>&#060;input type="Submit" value="Submit"&#062;<BR><BR><BR><BR><BR>&#060;%=to tal%&#062;<BR>&#060;/form&#062;<BR>

2. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: ASP Form Calculation

request.form("field1")<BR>Any request at all will always give you a "String" value...<BR>Yes you entered a number, and yes it looks like a number when written to page... But its really a string variant.<BR><BR>So you need to convert it to a real number...<BR>CInt or CLng (Depending on how big the number might get...<BR><BR>I useually make a habit of using Lng just so im sure not to exceed the int limits...<BR><BR>So do this to convert all forms to numbers<BR><BR>IF isNumeric(request.form("field1")) Then<BR> &#039;this was just a test to be sure its a valid number<BR> field1 = CLng( request.form("field1") )<BR>Else<BR> &#039;Oops this was not a valid number entered.<BR>End IF<BR><BR>You should always test, and validate every request.something<BR>to be sure it is exactly what your expecting it to be.

3. jpn
Senior Member
Join Date
Dec 1969
Posts
687

## build on from here

4. Senior Member
Join Date
Dec 1969
Posts
6,476

## Agreed BUT

Best to do the validationg from client, and save a trip to server if you can....<BR><BR>But even tho your doing it client side, you still have to do the same tests as you request them from asp...<BR>

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

## Russell and JPN *both* missed...

...the fact that the original poster *is* converting the strings to numbers. Here:<BR><BR> total = field1*1 + field2*1 + field3*1<BR><BR>Because he is *MULTIPLYING* the value from the form by 1, and since multiply only works with numbers VBScript will automatically do the equivalent of CDBL before doing the multiply.<BR><BR>NOW...<BR><BR>What that does *NOT* do is protect you against NON-NUMERIC values that are entered into the form!<BR><BR>So, time to DEBUG DEBUG DEBUG.<BR><BR><BR>&#060;% dim field1, field2, field3<BR>field1 = request.form("field1") <BR>field2 = request.form("field2")<BR>field3 = request.form("field3") <BR><BR>Response.Write "field1 has value " & field1 & " and is a number? " & IsNumeric(field1) & "&#060;P&#062;"<BR>Response.Write "field2 has value " & field2 & " and is a number? " & IsNumeric(field2) & "&#060;P&#062;"<BR>Response.Write "field3 has value " & field3 & " and is a number? " & IsNumeric(field3) & "&#060;P&#062;"<BR><BR>total = field1*1 + field2*1 + field3*1<BR>%&#062;<BR><BR>What does *THAT* show you?<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
6,476

## Have you been using

Puff&#039;s Plus to clean glasses again?<BR><BR>&#060;/snicker&#062;<BR>

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

## Simplify simplify simplify

First of all, it&#039;s ugly to repeat <BR> document.frmAdd<BR>all over the place. Why not do<BR> var frm = document.frmAdd;<BR>and then use just frm thereafter?<BR><BR>Similarly,<BR> var fld1 = frm.field1;<BR> var fld2 = frm.field2;<BR> etc.<BR><BR>*********<BR><BR>Also, you use <BR> onblur="return ...()"<BR>but your functions never return anything!<BR><BR>**********<BR><BR>SO... simplified:<BR><BR>&#060;SCRIPT&#062;<BR>function ReCalculate( fld )<BR>{<BR> var frm = fld.form;<BR> <BR> if( isNaN( fld.value ) )<BR> {<BR> alert("Please enter a numeric Value");<BR> fld.value = "";<BR> fld.focus( ); <BR> return false;<BR> }<BR> frm.totalField.value = parseFloat(frm.field1.value) + parseFloat(frm.field2.value) + parseFloat(frm.field3.value);<BR> return true;<BR>}<BR><BR>&#060;form action="untitled2.asp" method="post" name=frmAdd&#062; <BR>Field 1: &#060;input name="field1" onblur="return ReCalculate(this);"&#062;&#060;br/&#062;<BR>Field 2: &#060;input name="field2" onblur="return ReCalculate(this);"&#062;&#060;br/&#062;<BR>Field 3: &#060;input name="field3" onblur="return ReCalculate(this);"&#062; <BR>&#060;P&#062;<BR>Total: &#060;input name="total" readonly&#062;<BR>...<BR>&#060;/FORM&#062;<BR><BR>[And there&#039;s really no reason to do LANGUAGE=javascript *unless* you have both JS and *client-side* VBS on the same HTML page. JS is *always* the default language on homogeneous pages.]<BR><BR>

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

## Ooops...minor goof...

My JS and form field names don&#039;t agree. Should be<BR> Total: &#060;input name="totalField" readonly&#062;<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
•