How to Add Comma After Every 3 Digits in JavaScript

AuthorSumit Dey Sarkar

Pubish Date15 Jan 2024

categoryJavaScript

In this tutorial we will learn how to add comma after every 3 digits in JavaScript.

How to Add Comma After Every 3 Digits in JavaScript

Add commas to numbers in JavaScript

Follow the below steps to add commas after every three digit in javascript.

Step 1: Create input field with HTML

We are creating an HTML form with an input field for the amount, but you can modify it according to your needs.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Add Comma After Every 3 Digits in JavaScript</title>
</head>
<body>
  <section class="p-3">
    <div class="form-main-div">
      <img src="https://www.teknowize.com/front-assets/images/logo/logo.svg" alt="logo">
      <p class="heading">Total Amount</p>
      <form action="javascript:void(0)">
        <div class="form-inner-div">
          <div class="input-div">
            <input type="text" class="dollar_inp" placeholder="Amount" value="1,250,000">
            <div class="auth-left-icon">
              $
            </div>
          </div>
          <button type="submit" class="submitButton">Submit</button>
        </div>
      </form>
    </div>
  </section>
</body>
</html>

Step 2: Style HTML with CSS

In this step, we are styling the HTML which created above with the help of CSS but you can styling this according to your needs.

* {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    section {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    .heading {
      font-size: 24px;
      font-weight: 700;
      color: #333;
    }

    .form-main-div {
      background: #fdfffd;
      border: 1px solid #07e4885d;
      box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.03);
      border-radius: 8px;
      padding: 60px;
      text-align: center;
      margin-top: 30px;
      width: 640px;
    }

    .form-inner-div {
      position: relative;
      width: 100%;
    }

    .input-div {
      position: relative;
      max-width: 370px;
      margin: 0 auto;
      margin-top: 1rem;
    }

    .input-div input {
      width: 100%;
      border: 1px solid #027a48;
      border-radius: 5px;
      height: 50px;
      padding: 0 1.5rem 0 3.5rem;
      font-size: 18px;
      color: #1f1f1f;
    }

    .input-div input:focus {
      outline: none;
      box-shadow: none;
    }

    .input-div input::placeholder {
      font-size: 18px;
      color: #B0B0B0;
    }

    .input-div .auth-left-icon {
      position: absolute;
      left: 0;
      top: 0;
      width: 50px;
      height: 50px;
      background: #027a48;
      border: 1px solid #027a48;
      border-radius: 5px 0 0 5px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      font-size: 32px;
      font-weight: 700;
    }

    .clearInputIcon {
      position: absolute;
      top: 50%;
      right: 15px;
      transform: translateY(-50%);
      cursor: pointer;
    }

    .form-inner-div .submitButton {
      width: 250px;
      height: 50px;
      background: #00d17a;
      border-radius: 8px;
      font-weight: 600;
      font-size: 20px;
      text-align: center;
      color: #FFFFFF;
      margin-top: 25px;
      border: 1.5px solid #00d17a;
      cursor: pointer;
    }

    .form-inner-div .submitButton:hover {
      background: #ffffff;
      color: #00d17a;
    }

    @media (max-width:672px) {
      .form-main-div {
        width: 100%;
        padding: 25px;
      }
    }

Step 3: Add commas after every three digit with Javascript

Here we are adding commas after every three digit from last with the help of JavaScript.

// Add Comma after 3 digit
         function updateTextView(_obj) {
            var num = getNumber(_obj.value);
            if (num == 0) {
                _obj.value = "";
            } else {
                _obj.value = num.toLocaleString();
            }
        }
        function getNumber(_str) {
            var arr = _str.split('');
            var out = new Array();
            for (var cnt = 0; cnt < arr.length; cnt++) {
                if (isNaN(arr[cnt]) == false) {
                    out.push(arr[cnt]);
                }
            }
            return Number(out.join(''));
        }
        document.querySelectorAll('.dollar_inp').forEach(function (eachDollarInp) {
            eachDollarInp.addEventListener('input', function () {
                updateTextView(event.target)
                var key = event.keyCode;
                // Only allow numbers to be entered
                if (key < 48 || key > 57) {
                    event.preventDefault();
                }
            })
        })

Let's combine HTML, CSS, and JS together.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Add Comma After Every 3 Digits in JavaScript</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        section {
            width: 100%;
            display: flex;
            justify-content: center;
        }

        .heading {
            font-size: 24px;
            font-weight: 700;
            color: #333;
        }

        .form-main-div {
            background: #fdfffd;
            border: 1px solid #07e4885d;
            box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.03);
            border-radius: 8px;
            padding: 60px;
            text-align: center;
            margin-top: 30px;
            width: 640px;
        }

        .form-inner-div {
            position: relative;
            width: 100%;
        }

        .input-div {
            position: relative;
            max-width: 370px;
            margin: 0 auto;
            margin-top: 1rem;
        }

        .input-div input {
            width: 100%;
            border: 1px solid #027a48;
            border-radius: 5px;
            height: 50px;
            padding: 0 1.5rem 0 3.5rem;
            font-size: 18px;
            color: #1f1f1f;
        }

        .input-div input:focus {
            outline: none;
            box-shadow: none;
        }

        .input-div input::placeholder {
            font-size: 18px;
            color: #B0B0B0;
        }

        .input-div .auth-left-icon {
            position: absolute;
            left: 0;
            top: 0;
            width: 50px;
            height: 50px;
            background: #027a48;
            border: 1px solid #027a48;
            border-radius: 5px 0 0 5px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            font-size: 32px;
            font-weight: 700;
        }

        .clearInputIcon {
            position: absolute;
            top: 50%;
            right: 15px;
            transform: translateY(-50%);
            cursor: pointer;
        }

        .form-inner-div .submitButton {
            width: 250px;
            height: 50px;
            background: #00d17a;
            border-radius: 8px;
            font-weight: 600;
            font-size: 20px;
            text-align: center;
            color: #FFFFFF;
            margin-top: 25px;
            border: 1.5px solid #00d17a;
            cursor: pointer;
        }

        .form-inner-div .submitButton:hover {
            background: #ffffff;
            color: #00d17a;
        }

        @media (max-width:672px) {
            .form-main-div {
                width: 100%;
                padding: 25px;
            }
        }
    </style>
</head>

<body>
    <section class="p-3">
        <div class="form-main-div">
            <img src="https://www.teknowize.com/front-assets/images/logo/logo.svg" alt="logo">
            <p class="heading">Total Amount</p>
            <form action="javascript:void(0)">
                <div class="form-inner-div">
                    <div class="input-div">
                        <input type="text" class="dollar_inp" placeholder="Amount" value="1,250,000">
                        <div class="auth-left-icon">
                            $
                        </div>
                    </div>
                    <button type="submit" class="submitButton">Submit</button>
                </div>
            </form>
        </div>
    </section>
    <script>
        // Add Comma after 3 digit
        function updateTextView(_obj) {
            var num = getNumber(_obj.value);
            if (num == 0) {
                _obj.value = "";
            } else {
                _obj.value = num.toLocaleString();
            }
        }
        function getNumber(_str) {
            var arr = _str.split('');
            var out = new Array();
            for (var cnt = 0; cnt < arr.length; cnt++) {
                if (isNaN(arr[cnt]) == false) {
                    out.push(arr[cnt]);
                }
            }
            return Number(out.join(''));
        }
        document.querySelectorAll('.dollar_inp').forEach(function (eachDollarInp) {
            eachDollarInp.addEventListener('input', function () {
                updateTextView(event.target)
                var key = event.keyCode;
                // Only allow numbers to be entered
                if (key < 48 || key > 57) {
                    event.preventDefault();
                }
            })
        })
    </script>
</body>
</html>

Output

View live output here 👉 Input Field with Comma After Every 3 Digit in Amount using Javascript

How to Add Comma After Every 3 Digits in JavaScript

Comments 0

Leave a comment