屏蔽编辑单个输入文本框的两种代码格式
我有两个掩码 G999-9 和 H99-9,分别代表毕业生和荣誉,都需要输入单个输入文本 html 控件。
第一次打字时,如果它击中了 GI,我希望它显示研究生格式的格式,对于荣誉学位,我希望它在打字时显示荣誉学位的格式。有没有一种简单的方法可以在 javascript 或 jQuery 中做到这一点?
示例:
G111-1 H91-5 G001-3 G___-_ (If you hit G it should show the Graduate format) H__-_ (If you hit H it should show the Honors format.) ____-_ (Type anything else, or nothing do this.)
谢谢, 马克
I have two masks G999-9 and H99-9 which stand for graduates and honor which both need to be entered for a single input text html control.
While first typing, if it hits a G I would like it to show the format for the Graduate format, and for the Honors, I would like it to show the format for the Honors format while typing it out. Is there a simple way to do this in javascript or jQuery?
Examples:
G111-1 H91-5 G001-3 G___-_ (If you hit G it should show the Graduate format) H__-_ (If you hit H it should show the Honors format.) ____-_ (Type anything else, or nothing do this.)
Thanks,
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于正则表达式的演示
耶!正则表达式!是的,就像所有其他(非 HTML)解析问题一样,有一个正则表达式可以解决这个问题。 :)
这有点复杂(如果能帮助优化它,我们将不胜感激),但它确实有效。
Regex Based Demo
Yay! regex! Yes, like every other (non-HTML) parsing questions there's a regex for that. :)
It's a bit convoluted (help in optimizing it would be much appreciated), but it works.
简单的方法吗?您要么必须遍历字符串,要么使用一些奇特的正则表达式。
通常,我们会为此使用正则表达式,但假设您对正则表达式不太了解(也许是一个糟糕的假设,但大多数能够阅读它们的人会首先倾向于它们)并希望让您留住根据发生的事情,我更程序化地编写了它:
这是一个小提琴,甚至: http://jsfiddle.net/UdcND/
Simple way? You're either going to have to walk the string or use some fancy regex.
Normally, we'd use regexes for this, but with the assumption that you don't know so much about regexes (perhaps a bad assumption, but most people that can read them will gravitate towards them first) and in the hopes of keeping you following what is happening, I wrote it more procedurally:
Here's a fiddle, even: http://jsfiddle.net/UdcND/
有一些 jQuery 插件可以轻松地将文本输入转换为掩码编辑:
一个简单但有用的插件: http://www.pengoworks.com/workshop/js/mask/
<强>另一个不错的选择: http://digitalbush.com/projects/masked-input-plugin/
There are some jQuery plugins for easily converting a text input to a mask edit:
a simple but useful plugin: http://www.pengoworks.com/workshop/js/mask/
another good option: http://digitalbush.com/projects/masked-input-plugin/