Link to home
Start Free TrialLog in
Avatar of uri22
uri22

asked on

Enter Amount Text Field

Hi
I should present a text field that represents amount in USD.
This amount is combined of two parts left of the decimal point and right of the decimal point, e.g. 10.00.
I want the field to be represented when the ".00" is already printed on it so that the customer will only have to type the left part.
How should I do it with Java Script.
Please give me a full detailed example.
Thank you
uri22
ASKER CERTIFIED SOLUTION
Avatar of mattyk
mattyk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of a.marsh
a.marsh

Well that is a simple way of doing it mattyk.

However I'm suspecting there is a little more to this than that...

..is it the case uri22 that the .00 never changes or is the user able to change the value to the right of the decimal point?

Ant
yeah I was thinking that it would probably be more involved too but I figured why not start with the simplest solution and then take it from there :)

-matt
this should point you the right way.

if you'd like more validation, i can add that.

<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT>
function makeCurrency(form) {
  var dollars     = "";
  var value       = form.USD.value;
  var containsDot = false;
 
  for(var i=0;i<value.length; i++) {
    if (value.charAt(i) != ".") {
      dollars += value.charAt(i);
    }
    else {
      containsDot = true;
      dollars += value.charAt(i);
    }
  }
  if(!containsDot) {
    dollars += ".00";
  }
  alert(dollars);
}
</SCRIPT>
</HEAD>
<BODY>

<FORM NAME="TEST">
<INPUT TYPE="TEXT" SIZE="15" NAME="USD">
<INPUT TYPE="BUTTON" VALUE="CONVERT" onClick="makeCurrency(TEST)">
</FORM>
I wrote a function which converts a number into currency,
here it is
function showCurrency (number) {
  var Result = "";
  var negFlag = false;
  var n = parseFloat(number);
  if (n<0){
     n = -1*n;
     negFlag = true;
  }
  var trunc = parseInt(n);
  if (trunc>1000){
     while (trunc>1000){
          tempVal = parseInt(trunc%1000);
          if (tempVal == 0)
               tempVal = "000";
          else if
               (tempVal < 10) tempVal = "00"+tempVal;
                else if
                    (tempVal < 100) tempVal = "0"+tempVal;
          Result = "," + tempVal+Result;
          trunc=parseInt(trunc/1000);
     }
     Result = "" + trunc + Result + ".";
  }
  else {
     Result = "" + trunc + ".";
  }
  trunc = parseInt(n);
  n = (n - trunc) * 10;  
  trunc = parseInt (n);
  Result = Result +  trunc;
 
  n = (n - trunc) * 10;
  trunc = parseInt (n);
  Result = Result +  trunc;
  if (negFlag)
     return "$-"+ Result;
  else
     return "$"+ Result;
}  
<HTML>
<SCRIPT Language="JavaScript">
   function CheckVal(strAmount)
   {
          var     arrDollarsNCents;
          if ("" == strAmount.value)
               {
               strAmount.value = "0.00";
               return;
               }
     arrDollarsNCents=strAmount.value.split(".");
          if (2 != arrDollarsNCents.length)
               {
               strAmount.value += ".00";
               }
          if (2 == arrDollarsNCents.length)
               {
          while (2 > arrDollarsNCents[1].length)
                    {
                    arrDollarsNCents[1] += "0";
                    strAmount.value += "0";
                    }
               }
   }
</SCRIPT>
<BODY>
<FORM NAME=frmData>
<INPUT TYPE=TEXT NAME=strAmount SIZE=7 onBlur="CheckVal(this)"> </INPUT>
</FORM>
</BODY>
</HTML>
See comment from
   12/13/2000 12:10PM PST
Hello Triskelion,

You have been around long enough that you should know.  If there is valid expert comment before you post yours then you should restrict yourself to commenting instead of locking the question.  Remember the questioner has the ability to accept your answer if he/she feels it is the best one.

I am rejecting your answer on this basis.  uri22, please note that you can still accept this answer if it is the best one for you, but evaluate them on their merits please.

=============

Hi uri22

In the interest of maintaining the database of questions, it would be good if you could resolve your question.  Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. Award points to multiple experts--If you wish to award multiple participants, you can do so by creating a zero point question in the Community Support topic area, include this link and tell them which experts you'd like to award what amounts.

3. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

4. Delete the question because it is of no value to you or to anyone else. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, all you need to do is post right here as a comment, and I will take care of the rest.

PLEASE DO NOT AWARD THE POINTS TO ME. We also request that you review any other open questions you might have and deal with them as necessary.

Lunchy
Friendly Neighbourhood Community Support Moderator
Community Support:
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
There has been no activity in this question in quite some time, and it looks like it has been abandoned. As part of our ongoing mission to clean up the topic areas, a Moderator will finalize this question within the next seven (7) days. At that time, either I or one of the other Moderators will force/accept the comment of mattyk.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER. If you have further comments on this question or the recommendation, please leave them here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange
Force/accepted by

Netminder
Community Support Moderator
Experts Exchange