Solved

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

Posted on 2006-06-13
7
215 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

747 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

13 Experts available now in Live!

Get 1:1 Help Now