Solved

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

Posted on 2006-06-13
7
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Snap images to the bottom of a div 5 68
SP to delete duplicates 15 78
Problem to file 3 99
cookies analysis tools 2 111
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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