Jquery验证:带正/负的浮点数,小数点为6位2
Jquery Validation : Floating number filed with Positive/Negative with 6 digit 2 decimal
我试图限制我的文本字段,使其采用带有6个数字和2个小数限制的浮点数字。
示例123456.12222222.22.
我需要在小数点前最多有6位数字。数字可以是正数也可以是负数。
我尝试过:https://jsfiddle.net/wuL34dto/
$('.number').keypress(function(event) {
var $this = $(this);
if ((event.which != 46 || $this.val().indexOf('.') != -1) &&
((event.which < 48 || event.which > 57) &&
(event.which != 0 && event.which != 8))) {
event.preventDefault();
}
var text = $(this).val();
if ((event.which == 46) && (text.indexOf('.') == -1)) {
setTimeout(function() {
if ($this.val().substring($this.val().indexOf('.')).length > 3) {
$this.val($this.val().substring(0, $this.val().indexOf('.') + 3));
}
}, 1);
}
if ((text.indexOf('.') != -1) &&
(text.substring(text.indexOf('.')).length > 2) &&
(event.which != 0 && event.which != 8) &&
($(this)[0].selectionStart >= text.length - 2)) {
event.preventDefault();
}
});
提前谢谢。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.jasny.net/bootstrap/dist/js/jasny-bootstrap.min.js"></script>
<input class="inputmask">
<script>
$('.inputmask').inputmask({
mask: '999999.99'
})
</script>
text.match(^[0-9]{6}('.[0-9]{1,2})?$).length > 0
未测试此模式
或添加jquery函数
$.fn.extend({
checkinput:function() {
return text.match(^[0-9]{6}('.[0-9]{1,2})?$).length > 0;
});
用法:
if(text.checkinput())
相关文章:
- 将数字四舍五入到小数点后两位
- 将数字四舍五入到小数点后两位,以设置货币格式
- AngularJS {{ val | number:1 }} 不四舍五入到小数点后 1 位
- 使用AngularJS将HTML文件中的数字限制为小数点后2位
- 如何在javascript中获取no的小数点后三位
- 用Javascript显示价格,小数点后两位
- 小数点后两位四舍五入-找不到正确的公式
- 将值提交到小数点后 2 位
- 如何将结果固定为小数点后 2 位
- 如何在输入上转换为小数点后 2 位
- 如何将小数向上舍入到小数点后 n 位
- 将 JavaScript 数字转换为小数点后两位
- 将结果限制在小数点后 2 位
- 更改为在小数点后添加 2 位数字
- Javascript 将数字舍入到小数点后 2 位
- 将数字舍入到小数点后两位,并删除任何其他小数点
- 防止用户在小数点前 2 位数字后输入更多数字
- 在谷歌图表中以小数点后两位显示数字
- 在 Javascript 中难以将 json 对象值四舍五入到小数点后两位
- 如果为float,则显示小数点2位;如果为int,则显示十进制0位