Solved

How can I validate in a form an amount with two decimals like 12.98

Posted on 2006-06-13
7
219 Views
Last Modified: 2010-04-06
Hi Guys,
Does any one have a function to validate a textbox with format like 99.99?
Also, if the value doesn't have cents or decimals as soon as the cursor leaves the textbox it should add .00 to the amount.
I'm working with ASP.

Thanks
Carol

0
Comment
Question by:Carolinat
  • 5
  • 2
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 16895496
use this javascript function
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 16895501
if you don't want $ sign, use

<script language="javascript">
            function formatCurrency(num) {

     num = num.toString().replace(/\$|\,/g,'');
     if(isNaN(num))
          num = "0";
     sign = (num == (num = Math.abs(num)));
     num = Math.floor(num*100+0.50000000001);
     cents = num%100;
     num = Math.floor(num/100).toString();

     if(cents<10)
          cents = "0" + cents;
     for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
          num = num.substring(0,num.length-(4*i+3))+','+
          num.substring(num.length-(4*i+3));
   
     return (((sign)?'':'-')  + num + '.' + cents);
}

</script>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16895528
then call it from onchange event of a textbox:

<input type="text" name="text1" onchange="this.value = FormatCurrency(this.value);">
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Carolinat
ID: 16906575
Hi YZlat,
I'm using a control for the textbox like this:
<asp:textbox id="txtValor1" runat="server">
How can I call the javascript function from here?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16954494
txtValor1.Attributes.Add("onchange", "javascript:this.value = FormatCurrency(this.value);")
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16954497
place this code in Page_Load event
0
 

Author Comment

by:Carolinat
ID: 16962394
Excellent!!!
Thank you.
Carol
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 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