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

富古特98

我有一个带有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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何仅在ExtJS的文本字段中允许最多3个小数位

如何使用jQuery在文本字段中允许20到50之间的数字

表单输入文本字段中允许的字符数限制

如何创建仅在vaadin中支持数字的文本字段

仅在InputText字段中允许TypeAhead值

如何在文本字段中允许和获取Memoji Stickers

只允许在颤动文本字段中输入数字Flutter

我如何从文本字段中获取特定的数字范围?

在Java应用程序中仅在文本字段中输入数字

如何使用jQuery在输入文本字段中仅允许数字(即使复制粘贴时)?

如何从 Excel 中的文本字符串中提取特定数字?

如何从 Excel 中的文本字符串中提取特定数字?

在文本字段中基于特定数组键作为用户类型显示数组值

如何使用javascript允许一个特定单词进入表单的文本字段?

Swift - 如何限制几个特定的文本字段只允许插入 int?

计算文本字段中的数字

仅包含数字的文本字段-React

将文本字段限制为数字

我在根据输入到文本字段中的数字返回特定的输出时遇到麻烦

将文本字段中的数字与文本字段中的其他数字相加

如何创建特定的文本字段颤动?

在php中生成选定数量的文本字段

检查输入文本字段是否具有特定数值并使用 jquery 更改选择的选定选项

在列表中添加数字,但仅在特定数字之后

如何强制文本字段仅在Swift中为大写?

NSTextAligment仅在编辑文本字段后应用

阻止用户仅在文本字段中输入空格?

仅在文本字段上使用背景色

仅在提交时验证vuetify文本字段