c#⽂本框数字格式_数字⽂本框格式c# ⽂本框 数字格式
我有可以将HTML输⼊值格式化为数字格式或货币格式的脚本。希望它有⽤
function XFormat(arrayCtl)
{
this.arrCtl = arrayCtl;
if (typeof XFormat._initialized == 'undefined')
{
XFormat.prototype.setKeyPress = function(){return 0;};
XFormat.prototype.setFocus = function(){this.select();};
XFormat.prototype.setBlur = function(){return 0;};
XFormat.prototype.setFormat = function (){return 0;};
XFormat._initialized = true;
}
}
function XFormatNumber(arrayCtl)
{
XFormat.call(this,arrayCtl);
if (typeof XFormatNumber._initialized == 'undefined')
{
XFormatNumber.prototype.setKeyPress = function()
{
//mengizinkan karakter '-'(minus),'0-9'(numerik)
if(event.keyCode==45)
return true;
else if(event.keyCode>=48 && event.keyCode<=57)
return true;
else
return false;
};
XFormatNumber.prototype.setBlur = function()
{
if(this.value=='' || isNaN(this.value))
this.value = 0;
};
XFormatNumber.prototype.setFormat = function ()
{
if(this.arrCtl.length>0)
{
for(var i=0;i<this.arrCtl.length;i++)
{
this.arrCtl[i].Align='right';
this.arrCtl[i].onkeypress = this.setKeyPress;
this.arrCtl[i].onfocus = this.setFocus ;
this.arrCtl[i].onblur= this.setBlur;
}
}
};
XFormatNumber._initialized = true;
}
}
XFormatNumber.prototype = new XFormat();
function XFormatMoney(arrayCtl)
{
XFormatNumber.call(this,arrayCtl);
if (typeof XFormatMoney._initialized == 'undefined')
{
XFormatNumber.prototype.setKeyPress = function()
{
//mengizinkan karakter '-'(minus),'.'(separator desimal),'0-9'(numerik)
if(event.keyCode >=45 && event.keyCode<=46)
return true;
else if(event.keyCode>=48 && event.keyCode<=57)
return true;
else
return false;
};
XFormatMoney.prototype.setFocus =  OriginalValue;
XFormatMoney.prototype.setBlur = function()
{
var nStr = this.value+'';
var rgx = /(\d+)(\d{3})/;
var x;
if(this.value=='' || isNaN(this.value))
{this.value = '0.00';}
else
{
x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '.00';
while (st(x1))
{
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
if(x.length > 1 && x[1].length >2)
formatnumber数字格式x2 = '.'+x[1].substring(0,2);
this.value = x1 + x2;
}
};
XFormatMoney._initialized = true;
}
}
XFormatMoney.prototype = new XFormatNumber(); OriginalValue = function()
{
var originalValue=0;
var re = /,/;
if(this.value!='')
{
originalValue=place(re,'');
if(parseFloat(originalValue).length != originalValue)
originalValue = parseFloat(originalValue);
}
this.value = originalValue;
this.select();
}
这就是如何使⽤该功能
txtMoney1 = ElementById('txtMoney1');
txtMoney2 = ElementById('txtMoney2');
txtNumber1 = ElementById('txtNumber1'); txtNumber2 = ElementById('txtNumber2'); oXFormatNumber = new XFormatNumber([txtNumber1,txtNumber2]); oXFormatNumber.setFormat();
oXFormatMoney =new XFormatMoney([txtMoney1,txtMoney2]); oXFormatMoney.setFormat();
c# ⽂本框 数字格式

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。