Convert User Input to Number

Handy little snippet I just kicked together:

onblur="this.value=parseFloat(this.value.replace(/[^0-9.]/g,'')).toFixed(2);"

Just put it on any input element (or textarea if you really wanted to) and when a user is finished entering data, it will strip it down to a number, formatted to two decimal places.

Not that I typically advocate for writing javascript inline like this, but occasionally it’s the quick fix that the client wants!

Give it a demo here:

 

Author: George Stephanis

Cooking, Code, Carpentry, Letterpress.