Only float value with dot (not semicolon) on input - jQuery

I would like to always have a float value with a dot (not a semicolon) on 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, '.')); }) 

replace the comma with a dot, but it should not be> 1 dot and others ... It should only be accepted [0-9] and.

if I find a different value than the others [0-9] and. then this value should be deleted - ".".

How can i do this?

http://jsfiddle.net/ZtkBW/

+3
javascript jquery
source share
7 answers

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

The bit is different from your solution, but if you want to use it, it will not allow the characters text box to be printed.

The solution does a full number check, but for float it does not accept (period)

code example

 $('.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 });​ 
+10
source share

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

It would probably be wiser to store the value of $(this).val() in a variable, but I will leave this as an exercise for the reader. (you don’t have time now)

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

You can try the following:

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 not display the character if it is invalid. As mentioned in the comments, I also used focus. I do not understand if the last character entered is "." because you cannot enter a decimal value.

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

How does he behave

0
source share

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

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

I had the same problem - I need text input to accept only integer or decimal input (only one). I had to mix several solutions, but now it works:

 $(".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]; } }); 
0
source share

Try the following:

 $("#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(); } }); 
-one
source share

All Articles