I'm using <input type="number"...
, and I can't use comma? But I can't solve problem with changing it to <input type="text"
to use it. After it user could type some letters in input too and make some problems because that input is summed with other one.
Any suggestions?
Asked
Active
Viewed 4,077 times
0

proofzy
- 627
- 1
- 12
- 23
-
Please look at this solution http://stackoverflow.com/questions/15303940/how-to-handle-floats-and-decimal-separators-with-html5-input-type-number – Ala Eddine JEBALI Jan 14 '17 at 10:01
3 Answers
1
You want to use numbers like 1.2 or 2.3 in your input type number ? Then you can use the step attribute. Read the code below
<input type="number" step="0.01">

Joris
- 2,416
- 13
- 18
-
So step must be like this : step="0.01" I have edited my first comment, check it – Joris Jan 14 '17 at 10:11
-
-
What do you mean by can't use 55,55 ? can you explain what do you want to do ? – Joris Jan 14 '17 at 10:24
-
Well try to type some number, some number; example 50,06. In my country we use comma (,) for number. We do not use dot. – proofzy Jan 14 '17 at 10:29
-
I'm using firefox developper edition 52 and input type number using comma instead of dot. Check screenshots [dot](http://image.noelshack.com/fichiers/2017/02/1484390920-dot.png) and [comma](http://image.noelshack.com/fichiers/2017/02/1484390920-comma.png) – Joris Jan 14 '17 at 10:48
0
I hope it will work for you HTML Code
<span>Float</span>
<input type="text" name="numeric" class='allownumericwithdecimal'>
<div>Numeric values only allowed (With Decimal Point) </div>
<br/> <br/> <br/>
<span>Int</span>
<input type="text" name="numeric" class='allownumericwithoutdecimal'>
<div>Numeric values only allowed (Without Decimal Point) </div>
JavaScript code
$(".allownumericwithdecimal").on("keypress keyup blur",function (event) {
//this.value = this.value.replace(/[^0-9\.]/g,'');
$(this).val($(this).val().replace(/[^0-9\.]/g,''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
$(".allownumericwithoutdecimal").on("keypress keyup blur",function (event) {
$(this).val($(this).val().replace(/[^\d].+/, ""));
if ((event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
JS Fiddler Link Here Referance Link

Mohd Aman
- 224
- 1
- 3
- 13
-
-
Hey @proofzy, you have to implement a 'javascript function' for them – Mohd Aman Jan 14 '17 at 10:17
-
@proofzy try this code on keypres `function isNumber(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode(key); if (key.length == 0) return; var regex = /^[0-9.,\b]+$/; if (!regex.test(key)) { theEvent.returnValue = false; if (theEvent.preventDefault) theEvent.preventDefault(); } }` – Mohd Aman Jan 14 '17 at 10:26
-
Or try this, perfectly working and tested **HTML** `` **JavaScript** `function addComma(txt.value = this.value.replace(/[^\d,]/g,''))` – Mohd Aman Jan 14 '17 at 10:28
-
Well input need's to stay as number... Obviously chrome don't let me type number with comma... – proofzy Jan 14 '17 at 10:35
-
i have given to code that works perfectly in my last comment user that.. that is working fine – Mohd Aman Jan 14 '17 at 10:37
-
-
I am also using `Chrome browser` frnd ... see link here [js fidler](https://jsfiddle.net/wupmctrm/) – Mohd Aman Jan 14 '17 at 10:48
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133140/discussion-between-proofzy-and-md-aman). – proofzy Jan 14 '17 at 11:02
-
Well, my input type must be number, Because I have table in base with structure of "decimal(10,2)" and I can't change that – proofzy Jan 14 '17 at 11:09
0
Use this javascript and jquery script:
function transformTypedChar(charStr) {
return charStr == "," ? "." : charStr;
// HERE WE SET WHAT WE ARE GOING TO CHANGE
}
function getInputSelection(el) {
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function offsetToRangeCharacterMove(el, offset) {
return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
}
function setInputSelection(el, startOffset, endOffset) {
el.focus();
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
el.selectionStart = startOffset;
el.selectionEnd = endOffset;
} else {
var range = el.createTextRange();
var startCharMove = offsetToRangeCharacterMove(el, startOffset);
range.collapse(true);
if (startOffset == endOffset) {
range.move("character", startCharMove);
} else {
range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
range.moveStart("character", startCharMove);
}
range.select();
}
}
// change this one ID so script can work for that input
$("#changeMe").keypress(function(evt) {
if (evt.which) {
var charStr = String.fromCharCode(evt.which);
var transformedChar = transformTypedChar(charStr);
if (transformedChar != charStr) {
var sel = getInputSelection(this), val = this.value;
this.value = val.slice(0, sel.start) + transformedChar + val.slice(sel.end);
// Move the caret
setInputSelection(this, sel.start + 1, sel.start + 1);
return false;
}
}
});
And this is my HTML input:
<input type="text" autocomplete="off" step="any" font="red" style="width:120%" placeholder="changeMe" name="changeMe" class="input-sm form-control" id="changeMe" value="Change dot to comma">

proofzy
- 627
- 1
- 12
- 23