solomn517
вы можете использовать приведенный ниже javascript.
function isNumberDecimalKey(evt, ctlName, decimalPlaces) {
var key;
var isShift;
if (window.event) {
key = window.event.keyCode;
isShift = window.event.shiftKey ? true : false;
} else {
key = evt.which;
isShift = evt.shiftKey ? true : false;
}
if (isShift) {
switch (key) {
case 16:
case 37:
return false;
break;
default:
return false;
break;
}
}
//debugger;
var charCode = (evt.which) ? evt.which : event.keyCode;
var txtValue = document.getElementById(ctlName.id).value
//does not include current keypress
if (charCode == 8 || charCode == 9 || charCode == 46 || charCode == 37 || charCode == 39) // Backspace or tab or delete
return true;
if (charCode == 190 || charCode == 110) //decimal Not allowing decimals
{
if (txtValue.indexOf(".") != -1)
return false;
else
return true;
}
else if ((charCode > 57 || (charCode > 31 && charCode < 48)) && !(charCode >= 96 && charCode <= 105)) { //48-0, 96-numpad 0 Omitting both 0(zeroes)
return false;
}
else {
if (decimalPlaces != null && txtValue.indexOf(".") != -1) {
var txtSplit = txtValue.split(".");
var caretPosition = doGetCaretPosition(ctlName);
if (txtSplit[1].length == decimalPlaces && caretPosition > txtSplit[0].length) {
// if input is over the max decimal place, detect whether current selection existing.
var selectionText = "";
if (window.getSelection) {
selectionText = window.getSelection();
}
else if (document.selection) {
selectionText = document.selection.createRange().text;
}
return selectionText.length != 0;
}
}
return true;
}
}
function doGetCaretPosition(ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;
return (CaretPos);
}
вызов его метода OnKeyDown событие в текстовое поле
return isNumberDecimalKey(event, this, 2);
где 2-допустимое количество знаков после запятой