首页 > 其他 > 详细

input 价格格式校验

时间:2016-04-25 19:18:55      阅读:270      评论:0      收藏:0      [点我收藏+]

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>

 

 

input 价格格式校验

原文:http://www.cnblogs.com/fss226/p/5432167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!