Solved

Help Creating Currency Textbox with Javscript

Posted on 2011-09-24
8
365 Views
Last Modified: 2012-06-27
I am just trying to accomplish what seems to be a simple task, but am spending my entire day getting nowhere.

I'm looking for a script that will format numbers into a textbox as whole USD amounts and prevent any non-numbers from being entered. Also, whenever the user enters the first number, it's important that a dollar sign appear to the left of the total. After each 3 numbers, a comma will indicate the thousand's place.

E.g.

0 key -
1 key - $1
2 key - $13
3 key - $134
4 key - $1,345
5 key - $13,450
6 key - $134,500
7 key - $1,345,000
etc...

Can someone help put this together, please?
Thank you
0
Comment
Question by:k412m4z
  • 4
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 300 total points
ID: 36593429
hi,
here is the example check this....

Demo: http://jsfiddle.net/Sudhindra/fKvRZ/
code:
 
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo by Sudhindra</title>
  <script type='text/javascript' src='/js/lib/dummy.js'></script>
  
  <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  
  <style type='text/css'>
    
  </style>
  
  <script type='text/javascript'>
  //<![CDATA[ 
  
  function checkInput(ob){
    if(event.keyCode > 47 && event.keyCode < 58)
        return true;
    return false;
}

function formatIt(ob) {
    var vl=(trim(ob.value)).replace(/,/g,"");
    var finalRes = '';
    var ar= "";
    if(vl.charAt(0) == '$'){
        vl = vl.split(" ");
        vl=vl[1];
    }    
    ar = vl.split("");
    var k=0;
    for(var i=ar.length-1; i >= 0; i--) {
        k=k+1;
        finalRes = ar[i]+finalRes;
        if(k % 3 == 0 && k != ar.length) {
            finalRes = ","+finalRes;
        }
        ob.value = finalRes;
    }
    
    if(vl.charAt(0) != '$'){
        ob.value = '$ '+finalRes;
    }
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}


finalRes = "";
                            k=0;
                            for(var i=ar.length-1; i >= 0; i--) {
                                k=k+1;
                                finalRes = ar[i]+finalRes;
                                if(k % 3 == 0 && k != ar.length) {
                                    finalRes = ","+finalRes;
                                }
                            }
  
  //]]> 
  </script>
  
</head>
<body>
  <input type="text" id="cur" name="cur" value="" onkeypress="javascript:return checkInput(this)" onblur="javascript:formatIt(this)" />
  
</body>


</html>

Open in new window


BR-Sudhindra-www.clicksperday.com
0
 

Author Comment

by:k412m4z
ID: 36593480
Thank you ansudhindra. Looks good. Very close to what I am looking for...

Is there any way to make it so that the code will format AS the user types?

For instance...

If you start typing 3344 it shows as 3344

But only AFTER you click outside of the textbox it converts to $3,344

Get what I'm saying..? Not entirely necessary, but that is the overall goal here.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36593526
0
 

Author Comment

by:k412m4z
ID: 36593535
@leakim971

Anyway to convert this field to 'whole numbers' (no cents)?

Also, can it be 'blank' on load?

Functions well, otherwise.

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:k412m4z
ID: 36593539
So far really liking ansudhindra's solution...
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 200 total points
ID: 36593553
>Anyway to convert this field to 'whole numbers' (no cents)?
check the link, there's a small documentation to customize

updated : http://jsfiddle.net/uvevp/1/
0
 

Author Comment

by:k412m4z
ID: 36593637
@leakim971

Thanks for the update. I don't know why, but I'm having trouble implenting this on my site :/ I've spent too much time on this already, and don't want to hassle you any more into perfecting this.

Anyway... Both solutions are good, but I'm going to stick with ansudhindra's answer for my particular need, given that I've been able to implement it without any trouble - I made a small adjustment to restrict the field to numbers, but that is it. Works great.

I'm sure that folks will find both of these solutions useful in the future.

Thanks again for your time! Cheers
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36593650
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>
<script language="javascript" type="text/javascript" src="https://raw.github.com/plentz/jquery-maskmoney/master/jquery.maskMoney.js"></script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
		$("#currency").maskMoney({ "symbol":'$ ', "showSymbol":true, "decimal":',', "defaultZero":false, "precision":0 });
	})
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Find out what you should include to make the best professional email signature for your organization.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

20 Experts available now in Live!

Get 1:1 Help Now