自动添加字符的html文本框
大家好,我正在尝试制作一个 HTML 文本框: 1) 只接受数字 2) 3 位数字后添加“.” 3) 在 6 位数字后,再添加一个“.”
例如,只需输入数字,您就会得到 555.323.7637
4) 如果您在“.”之后,例如“555.”,则退格键会将其更改为“55”。
我一直在谷歌上搜索,但不知道我要找的确切术语。
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查看 MeioMask http://www.meiocodigo.com/ 它是一个 JQuery 插件,可以完成大部分功能你在追随。
Check out MeioMask http://www.meiocodigo.com/ it is a JQuery plugin that does most of what you are after.
这似乎是一种烦人的用户体验,但如果您坚持的话,您可以使用 onkeyup/onkeydown/onkeypress 侦听器(onkeyup 可能就足够了)。
如果您是用户 1) 了解格式,或 2) 该格式对于实际使用并不重要,则 1) 让他们格式化,或 2) 收到后格式化。
This seems like an annoying user experience, but if you insist then you can use the onkeyup/onkeydown/onkeypress listeners (onkeyup is likely sufficient).
If you're users 1) understand the format, or 2) the format is unimportant for actual use, then 1) let them format it, or 2) format it after receiving it.
尝试改变输入字段的工作方式是危险的领域。 通过使用一系列事件处理程序来侦听按键操作,您可能可以使其在 95% 的时间内正常工作。 另外百分之五的时间你的用户会咒骂你的名字。 考虑一下那些将复制并粘贴到文本框中的人(可能很多,因为它是 9 位数字?)。 我确信还有很多其他情况下正常事件不会按预期触发。
这是另一种可能有效的方法:当用户键入输入后输入模糊(即失去焦点)时,然后将屏蔽和格式应用于元素。 如果它再次获得焦点,则删除多余的标点符号,以便用户可以只处理数字。 例如:
Playing around with how input fields work is dangerous territory. By using a series of event handlers to listen for key presses you could probably make it work 95% of the time. The other five percent of the time your users will be cursing your name. Consider those who will copy and paste into the text box (probably quite a few since it's 9 digits?). I'm sure there's plenty of other cases where the normal events wouldn't fire as expected.
Here's another method which might work: when the input is blurred (that is, loses focus) after the user has typed their input, then apply your masking and formatting to the element. If it gets focus again, then strip out your extra punctuation so the user can just deal with the numbers. eg:
您想要做的可能涉及 Javascript 作用于相关文本字段的 onKey___ 事件,以验证和(如果必要)修改该字段的值。
为了修改退格键的行为,您可能还需要使用 onKeyDown 事件来考虑字段的内容。
另一个答案中提到的另一种选择是简单地使用 3 个单独的文本框 - 您可以使用 Javascript 使光标在每个文本框填满后自动“跳转”到下一个框,这是电话号码等内容的相当标准的解决方案输入。
What you're wanting to do would probably involve Javascript acting on the onKey___ events of the text field in question to validate and (if necessarily) modify the value of the field.
In order to modify the behavior of the backspace key, you'd probably also need to do things with the onKeyDown event to take into account the contents of the field.
An alternative option alluded to in another answer is to simply use 3 separate text boxes - you can use Javascript to make the cursor automatically "jump" to the next box after each one is filled, which is a fairly standard solution for things like phone number input.
您可以使用 Stack Overflow 在“您的答案”文本框中使用的相同方法。
您可以让用户在文本框中输入文本(仅使用 REGEX 验证数字字符),然后显示格式化的文本下面的文本,在另一个带有 onkeypress JavaScript 事件 http://www.w3schools.com/tags/att_input_readonly.asp" rel="nofollow noreferrer">只读属性,因此用户无法更改文本。
谢谢
You can use the same method that Stack Overflow uses in this very 'Your Answer' text box.
You can have the user enter their text in the text box (validate for numeric characters only using REGEX) and then display the formatted text below with the onkeypress JavaScript event in another text box with a read only attribute so the user cannot change the text.
Thanks
我以前尝试过这样做,但无法让它完全按照我想要的方式工作。 我发现捕获“keypress”事件(或者更好的是“keyup”事件)并重写该事件的字段值相当不错。 (这是大多数“屏蔽”库所做的,其他人已经发布了示例代码。)我将在这里重述的是我使用这种方法时遇到的问题:
(1)很难管理使用光标的人的问题键在字段中来回移动。 有关于如何以跨平台方式捕获光标键的注释,但它有点棘手。
(2) 处理删除键的按下更加痛苦(特别是因为他们可以将光标或单击到字段中的任何位置)。 当他们点击删除时仅仅删除光标之前的字符并不是一个好的用户体验:你会遇到人们无法删除内容的情况! 想象一下他们输入“123.4”,然后连续点击删除 6 次。 第一个删除删除“4”(到目前为止还不错),下一个删除删除“.”。 现在,您的格式规则会看到“123”,并且会添加“123”的小数点。 用户可以随意点击删除,它不会去任何地方! 我们通过删除删除前一个重要字符(您的情况下的数字)而不是前一个字符来解决此问题。
(3) 处理浆糊是我们从未解决过的问题。 通过“ctrl-v”粘贴会生成一个可以在不同平台上捕获的关键事件,但通过编辑菜单粘贴无法跨平台一致地捕获。 最终我们就放弃了。
我希望这些经验能有所帮助。 我很想分享我的代码,但不幸的是这是为了工作而我不被允许这样做。
I have tried doing this before, but was unable to get it working as completely as I wanted. I found that capturing the "keypress" event (or better yet "keyup" event) and re-writing the field value on that was FAIRLY good. (This is what most "masking" libraries do, and others have posted sample code already.) What I'll recount here is the PROBLEMS I had with this approach:
(1) It was difficult to manage the problem of people using the cursor keys to move back and forth through the field. There are notes out there on how to capture cursor keys in a cross-platform fashion but it's slightly tricky.
(2) Handling pressing of the delete key was even more of a pain (especially since they can cursor or click to anywhere in the field). Just removing the character before the cursor when they hit delete was NOT a good user experience: you would get situations where people couldn't delete stuff! Imagine they type "123.4", then hit delete 6 times in a row. The first delete removes the "4" (good so far), the next one removes the ".". Now your formatting rules see "123" and they add in the decimal for "123." The user can hit delete as often as they like and it won't go anywhere! We solved this by having delete remove the previous important character (digits in your case) instead of the previous character.
(3) Handling paste was one we never solved. Pasting via "ctrl-v" generate a key event that can be caught on different platforms, but pasting via the edit menu just couldn't be caught consistently across platforms. Eventually we just gave up on it.
I hope some of this experience helps. I'd love to share my code, but unfortunately it was for work and I'm not permitted to.
HTML:
JavaScript (JQuery):
这是快速编写的,没有经过测试,所以 YMMV 但这就是你可以做到的。
编辑:它似乎做你想做的事。 耸耸肩
除了数字之外,它不允许使用任何其他键(不允许退格键、空格、回车键、home/end 等)。 如果您需要它来做到这一点,请将其添加到您的问题中,以便我可以修改我的答案。
编辑2:退格键现在的工作方式如OP问题中所述。 删除有点棘手,留给OP作为练习。
HTML:
JavaScript (JQuery):
This was quickly written and not tested so YMMV but that is how you could do it.
EDIT: It appears to do what you want. shrug
It does not allow for any other keys but numbers (no backspace, spaces, enters, home/end, etc). If you need it to do that, please add that to your question so I can modify my answer.
EDIT 2: Backspace now works as described in the OP's question. Delete is a little trickier and is left as an exercise to the OP.