You can do replacement of ',' to '.' better inside of 'keyup' event handler with the following
$(this).val($(this).val().replace(/,/,'.'));
So you can use following dataEvents
dataEvents: [
{
type: 'keyup',
fn: function(e) {
var oldVal = $(this).val();
var newVal = oldVal.replace(/,/,'.');
if (oldVal !== newVal) {
$(this).val(newVal);
}
}
},
{
type: 'keypress',
fn: function(e) {
var key = e.charCode || e.keyCode; // to support all browsers
if((key < 48 || key > 57) && // if non digit
key !== 46 && key !== 44 && key !== 8 && // and not . or , or backspace
key !== 37 && key !== 39) { // arrow left and arrow right
return false;
}
}
}
]
On the following demo you can see the results live. The only disadvantage which I see in the example is following: if you will try to type comma in the middle of the text, the cursor position (caret) will be changed to the end of the input after the replacement comma to point. Probably it is not a real problem in your case. It you do want to save the cursor position you should probably do this
document.selection
using for IE or .selectionStart
and .selectionEnd
for Firefox.
UPDATED: I fixed the problem with the usage of e.keyCode
inside of keypress
event in the Firefox. I follows the information from here and use now e.charCode || e.keyCode
instead of e.keyCode
. The above code and the demo are fixed now.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…