带小数99.999 HTML块输入的最大值

时间:2017-10-31 19:32:41

标签: javascript html knockout.js

我正在尝试使输入仅使用值99.999。我不想使用MaxLength因为它不会计算小数位的长度。当它与特定regex不匹配时,我不想使用任何其他擦除功能。我希望它在输入中停止它。

 function IsCurrencyNoMinus1 (e, thisobj, min, max) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 44) || (keyCode == 46) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode))

var inStr = $(thisobj).val();
if (ret && (keyCode == 45) && ((thisobj.selectionStart != 0) || (inStr.indexOf('-') != -1)))
  ret = false;

if (ret && (keyCode == 46) && (inStr != '' && inStr.indexOf('.') != -1) && !(Math.abs(thisobj.selectionStart - thisobj.selectionEnd) == inStr.length)) {
  ret = false;
}

var dotPos = (inStr.indexOf('.') != -1) ? inStr.indexOf('.') : inStr.length;
inStr = inStr.replace(/\,/g, '');
var parts = inStr.split('.');
var maxParts = max.toString().split('.');

if (ret && (inStr != '' && (keyCode >= 48 && keyCode <= 57))) {
  if ((parts[0].length >= maxParts[0].length) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
    && (thisobj.selectionStart <= dotPos)) {
    ret = false;
  }

  if (ret && (parts[1] != undefined && parts[1].length >= 2) && ((thisobj.selectionStart - thisobj.selectionEnd) == 0)
    && (thisobj.selectionStart > dotPos) && (thisobj.selectionStart <= dotPos + 3))
    ret = false;

  var firstPos = thisobj.selectionStart < thisobj.selectionEnd ? thisobj.selectionStart : thisobj.selectionEnd;
  if (ret && (parts[0].length >= maxParts[0].length) && (parts[1] != undefined && parts[1].length >= 1)
    && ((dotPos - firstPos == 0 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4)
    || (dotPos - firstPos == 1 && (Math.abs(thisobj.selectionStart - thisobj.selectionEnd) >= 2 && Math.abs(thisobj.selectionStart - thisobj.selectionEnd) < 4))))
    ret = false;
}

if (Number(inStr) > max) {
  thisobj.value = '';
  ret = true;
}

if (Number(inStr) < min) {
  thisobj.value = '';
  ret = true;
}

// var re = new RegExp(/^\(?-?[0-9]{0,12}(\.[0-9]{0,2})?\)?$/)
// if (!re.test(inStr)) {
//    thisobj.value = ""
// }

return ret
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!如果有人需要,请检查下面的代码。

internal disk cache