最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 .

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

How can i make it?

/

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 .

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

How can i make it?

http://jsfiddle.net/ZtkBW/

Share Improve this question edited May 9, 2012 at 10:22 Tony Evyght asked May 9, 2012 at 10:17 Tony EvyghtTony Evyght 2,5756 gold badges21 silver badges19 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 11

Hiya demo http://jsfiddle.net/9Ry9t/ or http://jsfiddle.net/ZtkBW/2/ or http://jsfiddle.net/HJnLD/

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

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
});​

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

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)

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

You can try this :

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.

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

How it behave

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

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

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];
    }
});

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(); } 
    });
发布评论

评论列表(0)

  1. 暂无评论