文本框输入数字显示千位分隔符,大写提示
文本框输入数字显示千位分隔符,大写提示
//文本框
<input id="numId" />
<input type="text" style="width: 61%;border:none;margin-left:30%;color:#999;" id="numlbl" >
//js代码 --------------------start-----------------------------
//文本框输入数字时,调用以下两个方法
$('#numId').bind('input propertychange', function() {
$('#numId').val(thousandBitSeparator($(this).val()));
$('#numlbl').val(DX($('#numId').val()));
});
//千位分隔符
function thousandBitSeparator(num) {
var stmp = "";
if(num==stmp) return;
var ms = num.replace(/[^\d\.]/g,"").replace(/(\.\d{2}).+$/,"$1").replace(/^0+([1-9])/,"$1").replace(/^0+$/,"0");
var txt = ms.split(".");
while(/\d{4}(,|$)/.test(txt[0]))
txt[0] = txt[0].replace(/(\d)(\d{3}(,|$))/,"$1,$2");
return txt[0]+(txt.length>1?"."+txt[1]:"");
}
//显示大小写提示
function DX(num) {
var n = num.replace(/[^0-9]/g,'');
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
return "数据非法";
var unit = "千百拾亿千百拾万千百拾元角分", str = "";
n += "00";
var p = n.indexOf('.');
if (p >= 0)
n = n.substring(0, p) + n.substr(p+1, 2);
unit = unit.substr(unit.length - n.length);
for (var i=0; i < n.length; i++)
str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "整");
}