<asp:TextBox ID="txt2" runat="server" CssClass="decimalInput_R" onkeydown="decimalInput(this, 8, 5)" onkeyup="decimalFormat(this)"></asp:TextBox>
/*
==================================================================
decimal类型数据的输入检查
element:input text
dotLeft:整数部分位数
dotRight:小数部分位数
==================================================================
*/
function decimalInput(element, dotLeft, dotRight) {
if( (event.keyCode == 13 || event.keyCode == 8 || (event.keyCode == 110 && !hasChar(element.value, ".")))
|| (((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))
&& decimalLength(element.value, dotLeft, dotRight) )) {
event.returnValue = true;
} else {
event.returnValue = false;
}
}
/*
==================================================================
Decimal数据类型数据的格式化
==================================================================
*/
function decimalFormat(element) {
if ((event.keyCode >= 48 && event.keyCode <= 57)
|| (event.keyCode >= 96 && event.keyCode <= 105)
|| event.keyCode == 13|| event.keyCode == 8
|| event.keyCode == 110) {
var value = element.value;
if(value != "") {
var arr = value.split(".");
if(arr.length == 1) {
var arrInt = arr[0].split("");
var newValue = arrInt[arrInt.length-1];
var count = 0;
for(i = arrInt.length - 2; i >= 0 ; i--) {
if(arrInt[i] === ",") {
continue;
}
count++;
if(count === 3) {
newValue = "," + newValue;
count = 0;
}
newValue = arrInt[i] + newValue;
}
if(typeof(newValue) != "undefined") {
element.value = newValue;
}
return;
}
}
}
}