Javascript 输入中只有带点(不是逗号)的浮点值 - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10514106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:39:16  来源:igfitidea点击:

Only float value with dot (not comma) in input - jQuery

javascriptjquery

提问by Tony Evyght

I would like have always only float value with dot (not comma) in my inputs.

我希望在我的输入中始终只使用带点(不是逗号)的浮点值。

<input type="text" class="dot"> <br />
<input type="text" class="dot"> <br />
<input type="text" class="dot"> <br />

$('.dot').keydown(function(){
        $(this).val($(this).val().toString().replace(/\,/g, '.'));  
})

this replace comma to dot, but this should not be have > 1 dot and others... This should be accept only [0-9] and .

这将逗号替换为点,但这不应该有 > 1 个点和其他...这应该只接受 [0-9] 和 .

if i type a different value than other [0-9] and . then this value should be remove - ''.

如果我输入的值与其他 [0-9] 和 . 那么这个值应该是删除 - ''。

How can i make it?

我怎样才能做到?

http://jsfiddle.net/ZtkBW/

http://jsfiddle.net/ZtkBW/

回答by Tats_innit

Hiya demohttp://jsfiddle.net/9Ry9t/orhttp://jsfiddle.net/ZtkBW/2/orhttp://jsfiddle.net/HJnLD/

Hiya演示http://jsfiddle.net/9Ry9t/http://jsfiddle.net/ZtkBW/2/http://jsfiddle.net/HJnLD/

Bit different from your solution but if you keen to use this, it will not allow any charactersto type in the text box.

与您的解决方案略有不同,但如果您热衷于使用它,它将不允许characters在文本框中键入任何内容。

The solution does full validation for numbers and for float it will not take (dot)

该解决方案对数字和浮点数进行了全面验证,它不会采用(点)

code sample

代码示例

$('.number').keypress(function(event) {
    if(event.which < 46
    || event.which > 59) {
        event.preventDefault();
    } // prevent if not number/dot

    if(event.which == 46
    && $(this).val().indexOf('.') != -1) {
        event.preventDefault();
    } // prevent if already dot
});?

回答by Elliot Bonneville

Here's a really terrible if statement to circumvent the fact that parseFloatcan parse the first number it finds in a string and indexOfonly returns the first index of a character (not all indexes).

这是一个非常糟糕的 if 语句来规避parseFloat可以解析它在字符串中找到的第一个数字并且indexOf只返回字符的第一个索引(不是所有索引)的事实。

It would probably be smarter to store the value of $(this).val()in a variable but I'll leave that as an exercise to the reader. (don't have time right now)

将 的值存储$(this).val()在变量中可能会更聪明,但我将把它留给读者作为练习。(现在没时间)

if(!isNaN(parseFloat($(this).val()) && $(this).val().indexOf(".") > -1 && $(this).val().indexOf(".") == $(this).val().lastIndexOf(".")) { 
    // do something, your value is a valid float
}

回答by Feugy

You can try this :

你可以试试这个:

http://jsfiddle.net/ZtkBW/5/

http://jsfiddle.net/ZtkBW/5/

var parseInput = function(val) {
  var floatValue = parseFloat(val);
  return isNaN(floatValue) ? '' : floatValue;
}

$('.number').keyup(function(){
  var value = $(this).val()+'';
  if (value[value.length-1] !== '.') {
    $(this).val(parseInput(value));
  }
}).focusout(function(){
  $(this).val(parseInput($(this).val()+''));
})

I used keyup to avoid displaying the character if invalid. As mentionned in comments, I also used focusout. I do not parse if the last entered character is '.' because you will not be able to enter a decimal value.

如果无效,我使用 keyup 来避免显示字符​​。正如评论中提到的,我也使用了 focusout。如果最后输入的字符是 '.' 我不解析 因为您将无法输入十进制值。

回答by S R

I had the same problem - needed a text input to accept only an integer or a decimal input (only one .) Had to mix a few solutions, but this is working now:

我遇到了同样的问题 - 需要一个文本输入来只接受一个整数或一个小数输入(只有一个)。不得不混合一些解决方案,但这现在正在工作:

$(".dot").keyup(function(event){
    this.value=this.value.replace(/[^0-9.]/g,'');
    var parts=this.value.split(".");
    var decimal=false;
    for(var part in parts)
    {
        if(part==0) this.value=parts[part];
        if(part==1) this.value=this.value+"."+parts[part];
    }
});

回答by Denis Ermolin

$('.dot').keydown(function(){
    $(this).val($(this).val().toString().replace(/^[0-9]\./g, ',')
    .replace(/\./g, ''));
})?

How it behave

它的行为方式

回答by arahaya

I'm not really sure what you want to do
but how about this

我不确定你想做什么,
但这个怎么样

$(this).val(parseFloat($(this).val()) || 0); 

回答by Nilesh Bavliya

Try this :

尝试这个 :

$("#txtSellerPrice").keydown(function (e) { 
    if (e.keyCode == 110 && this.value.split('.').length == 2) 
    { 
        e.preventDefault(); 
    } 
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190,110]) !== -1 ||         (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) ||                   (e.keyCode >= 35 && e.keyCode <= 40)) {  return;  } if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) 
    { e.preventDefault(); } 
    });