Here is a helpful javascript and jquery script:
function modifyCharacter(charStr) {
return charStr == "," ? "." : charStr;
// Define the character substitution logic here
}
function getSelection(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");
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
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 offsetToCharMove(el, offset) {
return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
}
function setSelection(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 = offsetToCharMove(el, startOffset);
range.collapse(true);
if (startOffset == endOffset) {
range.move("character", startCharMove);
} else {
range.moveEnd("character", offsetToCharMove(el, endOffset));
range.moveStart("character", startCharMove);
}
range.select();
}
}
$("#changeMe").keypress(function(evt) {
if (evt.which) {
var charStr = String.fromCharCode(evt.which);
var transformedChar = modifyCharacter(charStr);
if (transformedChar != charStr) {
var sel = getSelection(this), val = this.value;
this.value = val.slice(0, sel.start) + transformedChar + val.slice(sel.end);
setInputSelection(this, sel.start + 1, sel.start + 1);
return false;
}
}
});
Below is an example of an HTML input field:
<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">