JavaScript isCurrency() failing
<p>hey guys should be a easy one...I have some javascript that is turning my input values into currency values.  Problem is it will fail if I try to type in .5   heres is my code:</p>

<pre><code>function handleCurrency(formName,fieldName)
{
    var enteredValue = document.forms[formName].elements[fieldName].value;

    if ( enteredValue.isCurrency() )
    {
        alert("This is currency " + enteredValue )
        // Put the nicely formatted back into the text box.
        document.forms[formName].elements[fieldName].value = enteredValue.toCurrency();
    }
}
</code></pre>

<p>jsp:</p>

<pre><code> &lt;td&gt;&lt;input type="text" name="replacementCost" onchange="handleCurrency('NsnAdd','replacementCost')" value="&lt;ctl:currencyFormat currency='${form.replacementCost}'/&gt;" onkeypress="javascript:return noenter();" &lt;c:if test="${!lock.locked}"&gt;disabled="disabled"&lt;/c:if&gt; /&gt;&lt;/td&gt;
</code></pre>

<p>How can I make it so that .5 is allowable also to be formatted?</p>

<p>custom javascript:</p>

<pre><code>var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
var patternWithoutCommas = new RegExp("^\\s*\\$?-?\\d+(\\.\\d{1,2})?\\s*$");

function stringIsCurrency()
{
if (patternWithoutCommas.test(this))
{
    return true;
}
else if (patternWithCommas.test(this))
{
    return true;
}
return false;
}

function stringToCurrency()
{
if (this == '') return this;

var str = this.replace(/[$,]+/g,'');

sign = (str == (str = Math.abs(str)));
str = Math.floor(str*100+0.50000000001);
cents = str%100;
str = Math.floor(str/100).toString();

if (cents&lt;10) cents = "0" + cents;

for (var i = 0; i &lt; Math.floor((str.length-(1+i))/3); i++)
{
    str = str.substring(0,str.length-(4*i+3))+','+
          str.substring(str.length-(4*i+3));
}

str = '$' + ((sign)?'':'-') + str + '.' + cents;

return str;
}

String.prototype.isCurrency = stringIsCurrency;
String.prototype.toCurrency = stringToCurrency;
</code></pre>

<p>basically it needs to allow .5 and not just 0.5</p>

<p>this needs to be updated:</p>

<pre><code>var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$"); 
</code></pre>
