Bootstrap input currency format example

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To show input currency number formated, Just use .toLocaleString('en-US', { style: 'currency', currency: 'USD' });. It will format your currency aumont and show in the input .

Today, I am going to show you. how to create input currency format in bootstrap with code example.

Table of contents

  • Includes bootstrap view
  • Includes bootstrap library
  • Define its class name
  • Included JS bootstrap
  • Add JavaScript in <script> tag

This article will guide you to adding input currency format in Bootstrap with example.

Step 1: Includes bootstrap view

To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your <head>.

<meta name="viewport" content="width=device-width, initial-scale=1">

Step 2: Includes bootstrap library

First of all, load the Bootstrap framework CSS into the head tag of your webpage.

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

Step 3: Define its class name

After that, create the input currency format with a class name type="text".

    <table>
        <!-- Row #1: Enter some Numeric Text Here -->
        <tr>
            <td>Enter your Number here </td>
            <td>&nbsp;</td>
            <td class="input-group">
                <input type="text" id="txtExampleBoxOne" class="form-control" onBlur="toFinalNumberFormat(this);"
                    placeholder="$#,###.00">
            </td>
        </tr>
        <!-- Row #2: Add some More Numeric Text -->
        <tr>
            <td>Enter another number </td>
            <td>&nbsp;</td>
            <td class="input-group">
                <input type="text" id="txtExampleBoxTwo" class="form-control" onBlur="toFinalNumberFormat(this);"
                    placeholder="$#,###.00">
            </td>
        </tr>
    </table>

Step 4: Included JS bootstrap

Now, load the Bootstrap JavaScript file before closing the body tag and done.

   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
       integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
       crossorigin="anonymous"></script>

Step 5: Add JavaScript in <script> tag

Add the following JavaScript in <script> tag after the <body> tag to specify .

          function toFinalNumberFormat(controlToCheck) {
            var enteredNumber = '' + controlToCheck.value;
            enteredNumber = enteredNumber.replace(/[^0-9\.]+/g, ''); 
            controlToCheck.value = Number(enteredNumber).toLocaleString('en-US', { style: 'currency', currency: 'USD' }); 
        }

Example.

Let’s look at the following example to understand how it basically works:

input-currency-in-bootstrap.html

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>Clipboard example</title>

    <script>
        function toFinalNumberFormat(controlToCheck) {
            var enteredNumber = '' + controlToCheck.value;
            enteredNumber = enteredNumber.replace(/[^0-9\.]+/g, ''); // remove any non-numeric, non-period character
            controlToCheck.value = Number(enteredNumber).toLocaleString('en-US', { style: 'currency', currency: 'USD' }); // Number(enteredNumber).toLocaleString('en'); // enteredNumber.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
        }
    </script>
</head>

<body>
    <table>
        <!-- Row #1: Enter some Numeric Text Here -->
        <tr>
            <td>Enter your Number here </td>
            <td>&nbsp;</td>
            <td class="input-group">
                <input type="text" id="txtExampleBoxOne" class="form-control" onBlur="toFinalNumberFormat(this);"
                    placeholder="$#,###.00">
            </td>
        </tr>
        <!-- Row #2: Add some More Numeric Text -->
        <tr>
            <td>Enter another number </td>
            <td>&nbsp;</td>
            <td class="input-group">
                <input type="text" id="txtExampleBoxTwo" class="form-control" onBlur="toFinalNumberFormat(this);"
                    placeholder="$#,###.00">
            </td>
        </tr>
    </table>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>

</body>

</html>

Check the output of the above code example.

Bootstrap, Input, Carrancy Number

All the best 👍