Help Creating Currency Textbox with Javscript

Posted on 2011-09-24
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.


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

Can someone help put this together, please?
Thank you
Question by:k412m4z
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
  • 4
  • 3
LVL 13

Accepted Solution

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

<!DOCTYPE html>
  <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'>
  <script type='text/javascript'>
  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(" ");
    ar = vl.split("");
    var k=0;
    for(var i=ar.length-1; i >= 0; i--) {
        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 = "";
                            for(var i=ar.length-1; i >= 0; i--) {
                                finalRes = ar[i]+finalRes;
                                if(k % 3 == 0 && k != ar.length) {
                                    finalRes = ","+finalRes;
  <input type="text" id="cur" name="cur" value="" onkeypress="javascript:return checkInput(this)" onblur="javascript:formatIt(this)" />


Open in new window

Author Comment

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.
LVL 82

Expert Comment

ID: 36593526
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.


Author Comment

ID: 36593535

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

Also, can it be 'blank' on load?

Functions well, otherwise.


Author Comment

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

Assisted Solution

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 :

Author Comment

ID: 36593637

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
LVL 82

Expert Comment

ID: 36593650
<script language="javascript" type="text/javascript" src=""></script>
<script language="javascript" type="text/javascript" src=""></script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
		$("#currency").maskMoney({ "symbol":'$ ', "showSymbol":true, "decimal":',', "defaultZero":false, "precision":0 });

Open in new window


Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

623 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