Notes
HTML
CSS
JS
Tools
Code Snippets
HTML Entity
About
Contact Us
Input Field with Comma After Every 3 Digit in Amount using Javascript
HTML
CSS
JS
Output
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Input field with comma after every 3 digit in amount using 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>
Copied
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Input field with comma after every 3 digit in amount using 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>
CSS
* { 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; } }
Copied
* { 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; } }
JS
// 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(); } }) })
Copied
// 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(); } }) })
Output
Our Courses
HTML 5
CSS
JavaScript
MySql
PHP
Laravel
Bootstrap
Programming Tutorial
PHP
JavaScript
HTML
Laravel
MySQL
JQuery
CSS
Git
Bootstrap
Vue JS
Android
Python
Server
Code Snippets
Chat Box
Range Slider
Radio Button
Select Box
Nav Bar
Other
Testimonial
Carousel
Logo
Loader
Lightbox
Forms
Popup
Table
HTML Course Code
HTML Entity
↑→ Arrows
$¢ Currency
Aö Letters
%+ Math
1¾ Numbers
&— Punctuation
©™ Symbols
Best Of
Internet
Technology
Health
Travel
Onine Tools
CSS Minifier
Text Converter
Age Calculator
Pincode Details
Whiteboard
PDF to Text
Gradient Generator
PX to REM Converter
REM to PX Converter
PX to EM Converter
EM to PX Converter
WEBP Converter
Latest & Upcoming movies
Bollywood
Hollywood
Tamil
Telgu
Bangla
Web Series
Notes
HTML
CSS
JS