Jquery validation for currency field
I 开发者_开发技巧have a textfield and i need to bring the currency validations for that.
For ex: if the user enters 1999 then it should display 1999.00 in that textbox.
and if he enters 19.50 then it should diplay 19.50 only.
Is this achievable using CSS? if Yes could you please tell me how to do that?
or else provide me Jquery validation for this.
FormatAsMoney(document.getElementById("yourElementId"), 10000000000, true);
function FormatAsMoney(txtbox, upperlimit, useDecimal)
{
nStr = stripComma(txtbox.value);
txtbox.value = FormatAsMoneyFromString(nStr, upperlimit, useDecimal);
}
function FormatAsMoneyFromString(nStr, upperlimit, useDecimal) {
if(!isNaN(nStr)&& nStr != "")
{
if (upperlimit != null && (nStr * 1) <= upperlimit)
{
var amount = nStr * 1;
nStr = amount.toString();
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = (typeof(x[1]) == 'undefined')?'00':x[1];
x2 = (x2.length == 1)?x2+'0':x2;
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
{
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
if (useDecimal)
{
return x1 + '.' + x2;
}
else
{
return x1;
}
}
}
return nStr;
}
function stripComma(money)
{
var retVal = ""
if( money != null )
{
retVal = money;
if(retVal.indexOf(',') != -1) // strip the commas
{
var strip = retVal.split(',');
retVal = "";
for(var y=0;y< parseInt(strip.length);y++)
{
retVal += strip[y];
}
}
if(retVal.indexOf(' ') != -1) // strip the spaces
{
var strip = retVal.split(' ');
retVal = "";
for(var y=0;y< parseInt(strip.length);y++)
{
retVal += strip[y];
}
}
}
return retVal;
} // end stripComma
精彩评论