AngularJS, JavaScript

Currency Inputs in AngularJS (1.x)

Currency inputs seem to be a forgotten area of standard libraries. Even the batteries included .NET MVC framework doesn’t really answer the question for you. They often are omitted completely, and the community variants are many and buggy. This soup of conflicting standards over a very basic problem brings us to the canonical problem: when is it time to write your own solution?

xkcd standards

AngularJS (1.x) has at least five different easy to find options (links. to. many. options. here.). Most of them are built off the built in formatters for currency or numbers. But in my last work project I found most of them unacceptable, at least to the product manager testing the solutions. So I created my own n+1 answer.

My solution based on what was already available can be found on jsFiddle.net.

Other solutions had either an inability to include decimals, or odd behavior when you used the backspace key, or wigged out when you deleted a decimal. They also often used client locale information to decide the formatting rules, which was unnecessary and potentially wrong for my solution. My answer still has an issue when deleting from the middle of the input, the cursor will jump back to the far right end after. But I considered that still a solid win considering the offerings available before.

In the end my PM was happy, our customer was happy, and I think our code was happy. Even if I had to reinvent the wheel one more time.

Before You Go

 

Was this post was incorrect or incomplete? Leave a comment.

Was this post helpful to you? Subscribe for emails on full stack web development.