码迷,mamicode.com
首页 > 其他好文 > 详细

input 价格格式校验

时间:2016-04-25 19:21:49      阅读:226      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!