标签:
1.html
<input id="input_num" type="text"/>
2.js
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script><script>
$(function() {
var NUM_REG = /^[0-9]\d*$/;
var toolFn = {
fmoney : function(s, n) {
n = n > 0 && n < 20 ? n : 2;
s = parseFloat((s + ‘‘).replace(/[^\d\.-]/g, ‘‘)).toFixed(n) + ‘‘;
var l = s.split(‘.‘)[0].split(‘‘).reverse(),
r = s.split(‘.‘)[1],
t = ‘‘;
for (var i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ‘,‘ : ‘‘);
}
return t.split(‘‘).reverse().join(‘‘) + ‘.‘ + r;
},
rmoney : function(s) {
return parseFloat(s.replace(/[^\d\.-]/g, ‘‘));
}
};
$(‘#input_num‘).focus(function() {
this.value = this.value ? toolFn.rmoney(this.value) : ‘‘;
});
$(‘#input_num‘).keyup(function() {
if (!NUM_REG.test(this.value)) {
$(‘#input_num‘).val(‘‘);
}
});
$(‘#input_num‘).blur(function() {
if (NUM_REG.test(this.value)) {
this.value = this.value ? toolFn.fmoney(this.value, 0) : ‘‘;
}
})
})
</script>
标签:
原文地址:http://www.cnblogs.com/fss226/p/5432167.html