I am trying to use below code in keypress
http://jsfiddle.net/S9G8C/203/
But i am getting error : 0x800a138f - JavaScript runtime error: Unable to get property 'createRange' of undefined or null reference.
i know i passing wrong value here,please help me.
var caratPos = getSelectionStart(this);
I am using like
$("[id*=txtPrice]").keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && (e.which != 46)) {
return false;
}
var charCode = (e.which) ? e.which : e.keyCode;
var number = this.value.split('.');
//just one dot
if (number.length > 1 && charCode == 46) {
return false;
}
//get the carat position
var caratPos = getSelectionStart(this);
alert(this.value + ' and ' + caratPos);
var dotPos = this.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 1)) {
return false;
}
return true;
});
function getSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart
}