• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

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

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
Carolinat
Asked:
Carolinat
  • 5
  • 2
1 Solution
 
YZlatCommented:
use this javascript function
0
 
YZlatCommented:
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
 
YZlatCommented:
then call it from onchange event of a textbox:

<input type="text" name="text1" onchange="this.value = FormatCurrency(this.value);">
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
CarolinatAuthor Commented:
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
 
YZlatCommented:
txtValor1.Attributes.Add("onchange", "javascript:this.value = FormatCurrency(this.value);")
0
 
YZlatCommented:
place this code in Page_Load event
0
 
CarolinatAuthor Commented:
Excellent!!!
Thank you.
Carol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now