Solved

Web Form manipulation

Posted on 2004-09-03
11
156 Views
Last Modified: 2011-09-20
I have a form that I built in asp.net with over 40 textboxes that take in mostly dollar amounts but also rates and number of days or miles.  Here is an example of javascript I have in place to work with three textboxes in my form;
<SCRIPT LANGUAGE="JavaScript">
function SetPDTotal() {
      var PDTotal = 0.00;
      var PDRate = 0.00;
      PDRate = document.getElementById("txtPDRate").value;
      PDRate = PDRate.toFixed(2);      
      document.getElementById("txtPDRate").value = PDRate.toFixed(2);
      PDTotal = document.getElementById("txtPDDays").value * document.getElementById("txtPDRate").value;
      document.getElementById("txtPDTotal").value = "$" + PDTotal.toFixed(2);      
                        }
</SCRIPT>

So the point is to make txtPDTotal = what's entered in PDDays * what's entered in PDRate and make it look like a currency.  This part works great but I also want whatever is entered into PDRate to be manipulated into a double, thus the line: PDRate = PDRate.toFixed(2);
When I run the program I enter a number in txtPDDays and then a number in txtPDRate, the txtPDTotal is popualated with the correct amount from the multiplication but txtPDRate is not formatted and I get the JS error at the line where I have PDRate = PDRate.toFixed(2);:
"Object doesnt support this property or method"
First I tried just doing document.getElementById("txtPDRate").value = document.getElementById("txtPDRate").value.toFixed(2);    but that gave the same error.  Ideas?  
0
Comment
Question by:jacobymatt
  • 6
  • 3
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Hi

Well, toFixed is Javascript1.5 meaning only IE5.5+ and NS6+. You might also want to try this:

PDRate = parseFloat(PDRate,10).toFixed(2);

Regards,
Zyloch
0
 

Author Comment

by:jacobymatt
Comment Utility
That gave the same error, and if you look at the code I posted I use .toFixed to format my PDTotal and that part works fine.  The code is nearly identical to what I'm doing to PDRate but its bombing out with PDRate.  So
 document.getElementById("txtPDTotal").value = "$" + PDTotal.toFixed(2);  works fine but
document.getElementById("txtPDRate").value = PDRate.toFixed(2);  gives me this error.  What gives?  I thought one might be influencing the other but I tried them in different orders and it's always the line with document.getElementById("txtPDRate").value = PDRate.toFixed(2); that gives this javascript error:
"Object doesnt support this property or method"
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
This is my test code:

Works like a charm:

<SCRIPT LANGUAGE="JavaScript">
function SetPDTotal() {
     var PDTotal = 0.00;
     var PDRate = 0.00;
     PDRate = document.getElementById("txtPDRate").innerHTML;
     PDRate = parseFloat(PDRate,10).toFixed(2);    
     document.getElementById("txtPDRate").innerHTML = PDRate;
     PDTotal = document.getElementById("txtPDTotal").innerHTML * document.getElementById("txtPDRate").innerHTML;
     document.getElementById("txtPDTotal").innerHTML = "$" + PDTotal.toFixed(2);    
                    }
</SCRIPT>
<div onclick="SetPDTotal();">jlsdf</div>
<div id="txtPDTotal">
5
</div>
<div id="txtPDRate">
5
</div>

All I did was switch value for innerHTML which shouldn't change anything. Because PDTotal was multiplied, it was considered a number already so you can do toFixed. However, PDRate was never considered a number, so parseFloat fixed things.
0
 

Author Comment

by:jacobymatt
Comment Utility
My html:
<SCRIPT LANGUAGE="JavaScript">
function SetPDTotal() {
      var PDTotal = 0.00;
      var PDRate = 0.00;
      PDRate = document.getElementById("txtPDRate").innerHTML;
      PDRate = parseFloat(PDRate,10).toFixed(2);      
      document.getElementById("txtPDRate").innerHTML = PDRate;       
      PDTotal = document.getElementById("txtPDDays").innerHTML * document.getElementById("txtPDRate").innerHTML;
      document.getElementById("txtPDTotal").innerHTML = "$" + PDTotal.toFixed(2);      
                        }
</SCRIPT>
and my code behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load      
        txtPDRate.Attributes.Add("onblur", "SetPDTotal();")
    End Sub

and now when I type a number in txtPDRate and then click out of it I get the js error "Unknown Runtime Error" at the line that I have "document.getElementById("txtPDRate").innerHTML = PDRate;"

Those div controls you used were just for testing, right?  And I don't have to let the new attribute for txtPDRate know that its new function is a javascript function?  
0
 

Author Comment

by:jacobymatt
Comment Utility
Anyone?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Sorry, I was in school and fell asleep at 7 last night. Anyways, what is the code your ASP ends up outputting, meaning after the page loads, use View Source and paste the HTML.
0
 

Author Comment

by:jacobymatt
Comment Utility
<HEAD>
            <title>RezApprove :: powered by ADTRAV</title>
            <SCRIPT language="JavaScript">
function SetPDTotal() {
      var PDTotal = 0.00;
      var PDRate = 0.00;
      PDRate = document.getElementById("txtPDRate").innerHTML;
      PDRate = parseFloat(PDRate,10).toFixed(2);      
      document.getElementById("txtPDRate").innerHTML = PDRate;       
      PDTotal = document.getElementById("txtPDDays").innerHTML * document.getElementById("txtPDRate").innerHTML;
      document.getElementById("txtPDTotal").innerHTML = "$" + PDTotal.toFixed(2);      
                                          }
            </SCRIPT>

then in my form:
<input name="txtPDRate" type="text" id="txtPDRate" tabindex="10" onblur="SetPDTotal();" style="width:64px;" />

Does the onblur="SetPDTotal()"'; need to have "javascript:" in front of it or does it know to look for it in the javascript already.  

And the js error is "unkown runtime error" at line: document.getElementById("txtPDRate").innerHTML = PDRate;

thanks for the help, this is becoming urgent!      
0
 

Author Comment

by:jacobymatt
Comment Utility
nevermind about putting "javascript:" before the function.  I was slow to see that it is obviously picking up on the function or it wouldn't be erroring on the js function.  When I removed the line document.getElementById("txtPDRate").innerHTML = PDRate;
just to see what would happen it gave the same error at the line :
document.getElementById("txtPDTotal").innerHTML = "$" + PDTotal.toFixed(2);    
so it looks like it's having issues writing these values to the textboxes; just thought that might help.
0
 

Author Comment

by:jacobymatt
Comment Utility
finally got it to work using this method:
<SCRIPT language="JavaScript">
function SetPDTotal(form) {
      var PDTotal = 0.00;
      var PDRate = 0.00;
      PDRate = eval(form.txtPDRate.value);
      PDRate = parseFloat(PDRate,10).toFixed(2);      
      form.txtPDRate.value = PDRate;       
      PDTotal = eval(form.txtPDDays.value) * PDRate;
      form.txtPDTotal.value = "$" + PDTotal.toFixed(2);
                                    }
            </SCRIPT>

go figure
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
Comment Utility
Closed, 100 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now