因为js不支持逆序环视,所以用(\d),在支持逆序环视的语言如php中,可以用(?<=\d),当然php不用这个,而用number_format函数.这个代码比网上那些循环加,的好得多
<input name="num" onfocus="normalFormat(this)" onblur="numberFormat(this)"/>
<script>
function normalFormat(input) {
input.value = input.value.replace(/,/g,'');
}
function numberFormat(input) {
if (isNaN(input.value) || !isFinite(input.value)) {input.value = "";return;}
var v = parseFloat(input.value).toFixed(2);
//var r = /(\d)(?=(\d\d\d)+\.\d\d)/g;
var r = /(\d)(?=(\d\d\d)+(?!\d))/g; //这行和上行效果是一样的
input.value = v.replace(r,"$1,");
}
</script>
|
阅读(1165) | 评论(0) | 转发(0) |