DEV Community

Cover image for Counting zero's sucks!!
Caroline Mwasigala
Caroline Mwasigala

Posted on

Counting zero's sucks!!

Let's get real, it sucks when things don't behave the way they should. User-friendliness is underrated when it comes to form input fields.

When providing a user with an input field to write a number and it doesn't get formatted as how normally numbers are, it gives the user another task of counting zero's, honestly it sucks 😠.

Tried searching for a solution online but couldn't find what I wanted 😩, that's when I decided to solve it myself and help others by compiling the solution into a library and place it in a place where anyone can find it.

Github link
NPM link

Demo

Demo

How did I do it ?!

Regular Expression
Regular Expression
Why reinvent the wheel?! 😦
Regards,

Oldest comments (5)

Collapse
 
dmachibya profile image
David Machibya

Amazing 🔥 🔥

Collapse
 
gwanchi profile image
Erick Chrysostom

Very useful, i recommend

Collapse
 
mellen profile image
Matt Ellen • Edited

Have you tried toLocaleString? That also solves the problem of knowing when to use , as the decimal point and . as the thousands separator.

Collapse
 
mwasigala profile image
Caroline Mwasigala

Hello Matt , thanks for pointing this out, yes i tried using it but didn't get the flexibility that was needed on the input field. It formatted the number but it took control of the cursor, that's why i opted regex.

Collapse
 
mellen profile image
Matt Ellen

Thanks for the insight.