使用 Javascript 或 jQuery 防止在文本字段中输入数字
Prevent numeric input in a text field using Javascript or jQuery
我要求避免在文本类型输入字段中输入数字。我的应用程序仅在 iPhone 和 Android.
等设备中使用
为此,我尝试使用以下代码:
$("#custName-info").keypress(function (event) {
alert(event.charCode);
var inputValue = event.charCode;
if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
在这里,代码在 iPhone 上工作得很好,但在 Android 设备上不起作用。在调查它时,我发现了以下信息:
- keypress 已弃用,因此应避免使用。但如果这是原因,为什么它可以与我的 iPhone 或 Chrome 设备模拟器一起使用?
- 人们建议使用以下三个替代事件之一:
keyup, keydown, input, change
- 我进一步发现这 3 个事件中的任何一个在我的设备中都不起作用,因为它们没有给我
charCode
来识别数字输入。
keyCode
上有一些东西,但后来我不明白 charCode
和 keyCode
有什么区别
所以我对这个问题的最后一句话是,"how can I can prevent user to input numerics in the text field? Web Application being used in iPhone and Android devices"。
这对你有用吗?
$("#custName-info").on("input",function(event) {
var inputValue = this.value;
console.log(inputValue);
this.value = this.value.replace(/[0-9]/g,"")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />
拜访一下 https://bugs.chromium.org/p/chromium/issues/detail?id=118639#c283
无效的原始答案:
我按照
中的建议用keypress
代替了keydown
,用charCode
代替了which
JavaScript KeyCode vs CharCode
FIDDLE - 仍然适用于 iOS
$("#custName-info").on("keydown",function(event) {
var inputValue = event.which;
console.log(inputValue);
if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />
我要求避免在文本类型输入字段中输入数字。我的应用程序仅在 iPhone 和 Android.
等设备中使用为此,我尝试使用以下代码:
$("#custName-info").keypress(function (event) {
alert(event.charCode);
var inputValue = event.charCode;
if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
在这里,代码在 iPhone 上工作得很好,但在 Android 设备上不起作用。在调查它时,我发现了以下信息:
- keypress 已弃用,因此应避免使用。但如果这是原因,为什么它可以与我的 iPhone 或 Chrome 设备模拟器一起使用?
- 人们建议使用以下三个替代事件之一:
keyup, keydown, input, change
- 我进一步发现这 3 个事件中的任何一个在我的设备中都不起作用,因为它们没有给我
charCode
来识别数字输入。 keyCode
上有一些东西,但后来我不明白charCode
和keyCode
有什么区别
所以我对这个问题的最后一句话是,"how can I can prevent user to input numerics in the text field? Web Application being used in iPhone and Android devices"。
这对你有用吗?
$("#custName-info").on("input",function(event) {
var inputValue = this.value;
console.log(inputValue);
this.value = this.value.replace(/[0-9]/g,"")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />
拜访一下 https://bugs.chromium.org/p/chromium/issues/detail?id=118639#c283
无效的原始答案:
我按照
中的建议用keypress
代替了keydown
,用charCode
代替了which
JavaScript KeyCode vs CharCode
FIDDLE - 仍然适用于 iOS
$("#custName-info").on("keydown",function(event) {
var inputValue = event.which;
console.log(inputValue);
if (!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="text" id="custName-info" />