3

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/

Tony Evyght
  • 2,385
  • 6
  • 20
  • 19

7 Answers7

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
});​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • this is perfect, but i have on problem - i can't use BACKSPACE and DELETE :) – Tony Evyght May 09 '12 at 10:34
  • Hiya @TonyEvyght just to confirm man you do not want backspace and delete to work in the text box right? because they are working at the moment, cheers! – Tats_innit May 09 '12 at 10:40
  • in Firefox doesnt working, but in chrome working... In Opera also not work – Tony Evyght May 09 '12 at 10:44
  • @TonyEvyght Hmm weird tried in safari it works! :) hmm can you do me a favor run this and see what alert do you get for delete and backspace key: http://jsfiddle.net/ZtkBW/9/ delete should be 8, Don't panic if dot doesn't work as I have changed the event from `key press` to `keydown` just to test why it is not working in the browser you mentioned. hope this helps, cheerios! – Tats_innit May 09 '12 at 10:50
  • thanks, i accept answer (i can't add +1). could you add delete and backspace to this example so that it was correctly? – Tony Evyght May 09 '12 at 10:58
  • Lol @TonyEvyght I made few more changes gimme 2 more mins bruv, back in a sec! see yous! – Tats_innit May 09 '12 at 11:16
  • @TonyEvyght almost there testing it now, I have changed the event hence few other things are changed! – Tats_innit May 09 '12 at 11:27
  • @TonyEvyght and done see here http://jsfiddle.net/HJnLD/ : event changed `keyDown` now and `dlete = 8, . = 190 & backspace is 46` handled accordingly rest code should be clear, take care, cheers – Tats_innit May 09 '12 at 11:31
  • @Tats_innit Hi. And in case if i input some value in textBox, save it some where (for example in db) and now after page-refresh i load this value again in textbox - why i can input any symbol after all?. – user2167382 Apr 11 '14 at 10:33
  • @user2167382 not sure what your question is but; *if I understand it correctly*if you are storing the value in db and reading in every page refresh depends on the operation and the data integrity, i.e. consider page-cycle and what that data is doing etc... also if its jsut a input box AJAX it and dont page refresh. `:)` hope this helps. – Tats_innit Apr 13 '14 at 21:27
  • 1
    THE BEST !, thanks – M. Pancadewa Dec 07 '21 at 06:54
2

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
}
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
2

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.

Feugy
  • 1,898
  • 14
  • 18
  • thanks, this is perfect, but i can type many dot (...) and click outside input - then many dot are still – Tony Evyght May 09 '12 at 10:36
  • Ok, I made some changes to valid also on focusout(): look at the JsFiddle. http://jsfiddle.net/ZtkBW/20/ – Feugy May 09 '12 at 17:05
0
$('.dot').keydown(function(){
    $(this).val($(this).val().toString().replace(/^[0-9]\./g, ',')
    .replace(/\./g, ''));
})​

How it behave

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0

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

$(this).val(parseFloat($(this).val()) || 0); 
arahaya
  • 1,010
  • 9
  • 11
  • This won't work because there could be more than one dot and this would still return a valid number. It would simply parse up to the second dot and take everything before it and convert that bit to a number. – Elliot Bonneville May 09 '12 at 10:38
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];
    }
});
S R
  • 157
  • 1
  • 2
  • 12
-1

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(); } 
    });
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33