如何使用Jquery限制文本框中的字母和特殊字符? [英] How to restrict alphabets and special characters in a text box using Jquery?
本文介绍了如何使用Jquery限制文本框中的字母和特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我如何使用jquery将字母和特殊字符(不包括-")限制到文本框中吗?
Can someone please show me how to restrict alphabets and special characters excluding ''-'' into a text box using jquery?
推荐答案
Wes Aday和我自己的问题.我可以建议发现这种解决方案的另一种方法:阅读键盘事件的原始jQuery文档.特别要注意return false
如何作为默认事件处理的阻止程序在事件处理程序上工作: http://api.jquery.com /keypress [^ ].
此jsFiddle显示了一个完整的解决方案: http://jsfiddle.net/ExceptionLimeCat/cmfNj [
Please see the comments to the question by Wes Aday and myself. I could suggest one more method of finding out such solutions: read original jQuery documentation of keyboard events. In particular, pay attention howreturn false
works on an event handler, as a blocker of default event handling: http://api.jquery.com/keypress[^].
This jsFiddle shows a complete solution: http://jsfiddle.net/ExceptionLimeCat/cmfNj[^].
—SA
<pre lang="cs">function allowOnlyNumber(eventInstance) {
eventInstance = eventInstance || window.event;
key = eventInstance.keyCode || eventInstance.which;
if ((47 < key) && (key < 58) || key == 8 || key == 189 || key == 109) {
return true;
} else {
if (eventInstance.preventDefault)
eventInstance.preventDefault();
eventInstance.returnValue = false;
return false;
}
}
我试过了.
I tried this which worked.
这篇关于如何使用Jquery限制文本框中的字母和特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文