三位整数两位小数掩码,jQuery输入掩码小数点

I am using the jQuery plugin found at https://github.com/RobinHerbots/jquery.inputmask but I can't seem to be able to derive a pattern that corresponds to a integer & a decimal. For example,

I need the following to be valid:

10

150

12.25

0.45

At the moment I am doing:

$('#from_id').inputmask("9{0,5}.9{0,2}");

But this means that if the user does not specify what comes after the decimal point, the resulting output is:

For example, if the user only wants to input 12 (and he does not specify the decimal point) the output is

12___.__

(as the mask is waiting for the decimal point)

But if the user specifies the decimal point, for example 12.00, The output is fine like:

12.00

Could someone help me with this problem?

解决方案

Try to use the optional parmaneter greedy like,

$('#from_id').inputmask({'mask':"9{0,5}.9{0,2}", greedy: false});

You can validate above mask. Or use your own logica to validate like,

$(function(){

$('#id').on('keyup', function(e) {

if (!this.value.match(/^\d{0,5}(\.[0-9]{1,2})?$/)) {

$(this).addClass('error');// adding error class

} else {

$(this).removeClass('error');// remove error class

}

});

});

Or simply use toggleClass like,

$(function() {

$('#id').on('keyup', function(e) {

$(this).toggleClass('error', !this.value.match(/^\d{0,5}(\.[0-9]{1,2})?$/));

});

});

.error {

color: red;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值