如何使用密钥代码只允许将某些数字输入文本框

如何使用密钥代码只允许将某些数字输入文本框

本文介绍了如何使用密钥代码只允许将某些数字输入文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! var specialKeys = new Array(); specialKeys.push(8); // Backspace 函数IsNumericss(e){ var keyCode = e.which? e.which:e.keyCode var ret =((keyCode> = 49&& keyCode< = 57)|| specialKeys.indexOf(keyCode)!= -1); 返回ret; } 这只允许1-9号码,但我还需要允许这段时间,或者键盘代码110& 190. 我该如何做到这一点? 我尝试过类似的东西,但无济于事: var ret =((keycode> = 49&& keycode< = 57&& keycode == 110&& keycode == 190) 解决方案 这里有很好的解释: http://www.javascripter.net/faq/keyboardinputfiltering.htm [ ^ ]。 您需要添加额外的支票。 ret =( 49 < = keyCode&& keyCode< = 57 )|| ( 110 < = keyCode&& keyCode< = 190 )|| ( 8 == keyCode); 请注意,我还删除了多余的外部br ackets并排除了数组的使用,你只是用它来检查退格(8),这没什么意义。保持简单。 -SA var specialKeys = new Array(); specialKeys.push(8); //Backspace function IsNumericss(e) { var keyCode = e.which ? e.which : e.keyCode var ret = ((keyCode >= 49 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1); return ret; }This allows numbers 1-9 only but I need to allow the period also or keycodes 110 & 190.How would I go about doing this?I tried something like this but to no avail:var ret = ((keycode >= 49 && keycode <= 57 && keycode == 110 && keycode == 190) 解决方案 This is well explained here: http://www.javascripter.net/faq/keyboardinputfiltering.htm[^].You need to add an additional check.ret = (49 <= keyCode && keyCode <= 57) || (110 <= keyCode && keyCode <= 190) || (8 == keyCode);Note that I also removed redundant outer brackets and excluded the use of the array, which you used just for the check for backspace (8), which makes little sense. Keep it simple.—SA 这篇关于如何使用密钥代码只允许将某些数字输入文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 05:34