繁体   English   中英

仅允许文本字段中的特定数字

[英]Only allow specific numbers in text field

我有一个带有HTML表单的脚本,里面有一个文本字段,现在这个文本字段只允许数字。

虽然我希望文本字段只允许数字,并且只允许变量+1的当前值。 例如,假设变量(currentValue)的当前值为1,则可以放入文本字​​段的唯一数字是2。

这是我到目前为止的代码:

HTML:

<form id="value-form" action="value.php" method="POST" name="value-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" /> <!-- This is the text field I would like to only allow the value+1 -->
<input type="image"  src="Button1.png" id="customButton" value="submit" alt="but"/>
</form>

使用Javascript:

function isNumberKey(evt) //This is the function I use to only allow numbers in the text field
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}

var request = new XMLHttpRequest();
request.open('GET', 'currentValueFile.txt', false);
request.send();

var currentValue = request.responseText //this is the variable value I want to only allow +1 in the text field
document.getElementById("currentValue").innerHTML = currentValue;
<form id="value-form" action="value.php" method="POST" name="value-form">
<input onkeypress="return isNumberKey(event)" type="text" name="amount" id="amount" /> <!-- This is the text field I would like to only allow the value+1 -->
<input type="image"  src="Button1.png" id="customButton" value="submit" alt="but"/>
</form>

<script type="text/javascript">
function toDec(code) {
  return code - 48;
}
function isNumberKey(evt) //This is the function I use to only allow numbers in the text field
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57 || toDec(charCode) != currentValue + 1))
      return false;

    return true;
}

</script>

<script type="text/javascript"> 
var request = new XMLHttpRequest();
request.open('GET', 'currentValueFile.txt', false);
request.send();

var currentValue = parseInt(request.responseText) //this is the variable value I want to only allow +1 in the text field

</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM