/commas

Adds comma delimiters to numbers typed in to a text area or input

Primary LanguageJavaScript

commas.js

Synopsis

JavaScript Immediately-Invoked Function Expression (IIFE) automatically adds comma delimiters to numbers typed in to an HTML textarea or input element. This JavaScript automation is not as easy as it looks! Give it a try and be sure and partially backspace / delete over previously formatted numbers to see that formatting works in both directions.

Code Example

A working example of the code is available at https://rack.pub/commas.

Motivation

Some asshole on Stack Overflow asked for help with this so I created the solution. A non-working answer got accepted and I never got a single upvote. The JavaScript ended up being so difficult that I felt it was worthy of posting to my collection of JavaScripts.

Installation

🏁 Load via rack.pub's global CDN

<script src="https://rack.pub/commas/commas.min.js"></script>

🚀 Embed commas.min.js in your javascript when putting in production for best page loading performance.

API Reference

The auto formatting of numbers is invoked by adding the class commas, e.g. class="commas", to one or more HTML textarea's or input's.

Contributors

Ron Royston

License

Ron Royston, https://rack.pub, MIT License