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

查看:107
本文介绍了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天全站免登陆