jquery 只允许输入浮点数 [英] jquery only allow input float number

查看:26
本文介绍了jquery 只允许输入浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一些只允许浮点数的输入掩码.但当前的问题是我无法检查是否输入了多个点.你能帮我检查一下那些点并阻止它吗?

i'm making some input mask that allows only float number. But current problem is I can't check if multiple dots entered. Can you check those dots and prevent it for me?

实时代码:http://jsfiddle.net/thisizmonster/VRa6n/

$('.number').keypress(function(event) {
    if (event.which != 46 && (event.which < 47 || event.which > 59))
    {
        event.preventDefault();
        if ((event.which == 46) && ($(this).indexOf('.') != -1)) {
            event.preventDefault();
        }
    }
});

推荐答案

您可以在同一语句中检查句点.

You can check for the period in the same statement.

另外,你需要使用val方法来获取元素的值.

Also, you need to use the val method to get the value of the element.

另外,你要检查间隔48到57,而不是47到59,否则你也会允许/:.

Also, you want to check for the interval 48 to 57, not 47 to 59, otherwise you will also allow /, : and ;.

$('.number').keypress(function(event) {
  if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
    event.preventDefault();
  }
});

这篇关于jquery 只允许输入浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆