使用 jQuery 检测大写锁定开/关
如何使用 jQuery 检测 Caps Lock 键的开/关?我有一个密码文本框
,并且只允许使用小写字母,因此我不希望打开 Caps Lock 键。
是否可以使用 jQuery 检测 Caps Lock 键的状态?
How can I detect the Caps Lock key on/off using jQuery? I have a password textbox
, and I allow only lowercase letters so I don't want the Caps Lock key to be on.
Is it possible to detect the state of Caps Lock key using jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如何使用 Javascript 检测 Caps Lock。
然后输入您的密码表单:
How to detect Caps Lock with Javascript.
Then for your password form:
有一个名为 capslockstate 的 jQuery 插件,它将监视大写锁定键的状态整个页面,而不仅仅是特定字段。
您可以查询大写锁定键的状态或定义事件侦听器以对状态更改做出反应。
与此处的其他建议相比,该插件在检测和状态管理方面做得更好,包括使用非英语键盘、监视 Caps Lock 键本身的使用,以及在键入非字母字符时不会忘记状态。
有两个演示,一个显示基本事件绑定,另一个仅当密码字段具有焦点时才显示警告。
例如
和
插件的代码可以在 GitHub 上查看。
There is a jQuery plugin called capslockstate that will monitor the state of the caps lock key over the entire page, not just in specific fields.
You can either query the state of the caps lock key or define event listeners to react to state changes.
The plugin does a better job of detection and state management than the other suggestions here, including working with non-English keyboards, monitoring the use of the Caps Lock key itself, and not forgetting the state if non alpha characters are typed.
There are two demos, one showing basic event binding and another showing the warning only when the password field has focus.
e.g.
and
The code for the plugin is viewable on GitHub.
但你忘记了一些事情。如果您按 CapsLock 和 Shift 键并键入,则不会出现“Caps is on”消息。
这是一个更正的版本:
But you forgot something. If you press capslock and shift and type, there won't be the message 'caps is on'.
Here is a corrected version:
我找到了一个更好的方法来使用jquery来做到这一点:这样你就可以检测用户何时按下大写锁定,用户不需要输入字母来检查:(用户需要输入至少1个键来开始检测大写锁定)
演示: http://arthurfragoso.onphp.net/codes/capslock.html
I found a better way to do this using jquery: this way you can detect when the user press capslock, the user doesn't need to type a letter to check: (the user needs to type at least 1 key to start detecting the capslock)
demo: http://arthurfragoso.onphp.net/codes/capslock.html
发出警告
。只允许使用较小的字母是一个非常糟糕的主意。通过这样做,您将大大减少可能的密码数量。
What I do is put up a warning when
It's a pretty bad idea to only allow smaller letters. You're cutting down the number of possible passwords by a tremendous amount by doing that.
用户创建密码后,当他们在登录期间输入密码时,您可以在服务器上将其转换为小写,然后再检查密码是否正确。
这样可以为用户节省精力。
After the user has created their password, when they’re entering it during login, you could just convert it to lowercase on the server before checking whether it’s correct.
Saves effort for the user that way.