Creating Currency Formatter Javascript Input
<p>I have searched all around, and not been able to find the answer I have been looking for. I have found one answer that was achieved with AngularJS, but I'm not sure how to implement that. I have created a javascript function that formats an input field while a user is typing. It will be used for entering a dollar amount. It functions as follows: <br><br>
1) User types 1: <code>.1</code> -Not ideal, would like it to be 0.01, but it's working for most part<br>
2) User types 2: <code>.12</code><br>
3) User types 5: <code>1.25</code><br><br>
I have messed around and achieved the desired outcome through this code: </p>

<pre><code>&lt;script type="text/javascript"&gt;
function format_num(id) {
    var number = document.getElementById(id).value;
    number = number.replace(".","");
    number = number.replace(/\D/g, "");

    if (number.length == 0) {
     number = ""
     } else { 

        if (number.length &lt;2) {
            number = [".", number].join('');
        } else {
            number = [number.slice(0, (number.length-2)), number.slice(number.length-2)].join('.');
        }
    }
        document.getElementById(id).value = number;
}
&lt;/script&gt;
</code></pre>

<p><br> And it is called here: <br>
<code>&lt;input class="number" name="balance" id="balance" value="" onkeyup="format_num(id)" /&gt;</code><br><br>
I tried declaring my input field as type"number" but I found that it worked in Chrome, but not Safari. The only downside of this is the lack of a "0.xx" when the number is less than 3 digits long (i.e.: 0.01 or 0.10). Is this code safe for use? Thanks!</p>