繁体   English   中英

jQuery验证:以正数/负数和六位数2十进制归档的浮点数

[英]Jquery Validation : Floating number filed with Positive/Negative with 6 digit 2 decimal

我试图限制我的文本字段采用6位数字和2位小数限制的浮点数。

示例123456.12,222222.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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM