javascript 仅接受数字和破折号字符的输入文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19785288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input text field that only accepts numbers AND the dash character
提问by MillerMedia
I'm using the commonly used Javascript function to allow only numbers to be inputted into a text field:
我正在使用常用的 Javascript 函数来只允许将数字输入到文本字段中:
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
I call this onkeypress and it prevents anything but numbers to display. I'm trying to alter it so it will allow me to also put dashes (-) into the text field. The dash keycode is 189 so I tried this:
我将其称为 onkeypress,它可以防止显示除数字之外的任何内容。我正在尝试更改它,以便我也可以将破折号 (-) 放入文本字段。破折号键码是 189 所以我试过这个:
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode != 189 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
Thinking that the conditional statement would then accept the dash character but that didn't seem to work. Any ideas on why this would be? Thanks for your help!
认为条件语句会接受破折号字符,但这似乎不起作用。关于为什么会这样的任何想法?谢谢你的帮助!
回答by nnnnnn
If you're using the keypress event you need to use the character code 45 for dash/hyphen.
如果您使用 keypress 事件,则需要使用字符代码 45 作为破折号/连字符。
If you're using the keydown/keyup events thenyou need to use 109 and189 to cover the minus key in the numeric keypad and the one (usually) located above the P key.
如果您使用 keydown/keyup 事件,那么您需要使用 109和189 来覆盖数字小键盘中的减号键和位于 P 键上方的减号键(通常)。
if (charCode != 46 && charCode != 45 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
Demo: http://jsfiddle.net/J6B7U/
演示:http: //jsfiddle.net/J6B7U/
If you have doubts about which keycode is which a console.log(charCode);
in your function will help you debug.
如果您对console.log(charCode);
函数中的哪个键码有疑问,哪个 a可以帮助您调试。
(Note also that trapping a key event is not enough to prevent invalid data being entered, because the user may change the field using the browser's edit menu or drag'n'drop.)
(还要注意,捕获键事件不足以防止输入无效数据,因为用户可能会使用浏览器的编辑菜单或拖放来更改字段。)
回答by Ya?ar Xavan
try this
试试这个
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 45 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
in html
在 html 中
<input type="text" onkeypress="return isNumberKey(event)">
回答by Guffa
You want to use the character code for the dash, which is 45, not the keycode.
您想使用破折号的字符代码,即 45,而不是键代码。
回答by Jay Sudo
This doesn't work well with mobile devices. For example, Chrome on Android will bring up the number keypad rather than the full keyboard when the input type is set to "number"
. You could try using input type="tel"
instead. It would allow numbers 0-9
, the -
and ()
. It would also bring up the dial pad on Android. Haven't tested on iPhone.
这不适用于移动设备。例如,当输入类型设置为 时,Android 上的 Chrome 将显示数字键盘而不是全键盘"number"
。您可以尝试使用input type="tel"
代替。它将允许数字0-9
、-
和()
。它还会在 Android 上调出拨号盘。没有在 iPhone 上测试过。
回答by mehul
this code for only number types
此代码仅适用于数字类型
<input onkeypress="return isNumberKey(event);">
<script>
function isNumberKey(evt)
{
var t = (evt.which) ? evt.which : event.keyCode;
return !(t > 31 && (t < 48 || t > 57))
}
</script>