在用户使用JavaScript键入值时,如何验证文本框
我有一些麻烦来验证用户输入时的验证。
例如:有效范围是600-800,用户试图键入700
When textbox is empty: show nothing
When textbox is 7: show red
When textbox is 70: show red
When textbox is 700:show nothing
我希望我能在JS中做到这一点,谁能帮助我?
I have some troubles to validate user input while they are typing.
For example: the valid range is 600-800 and user is trying to type 700
When textbox is empty: show nothing
When textbox is 7: show red
When textbox is 70: show red
When textbox is 700:show nothing
I hope I can do it in js, can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个示例:
Here is an example:
可以使用on Keyup事件,在此处查看
keyp up event
The on keyUp event can be used for this, have a look here
Keyp up event
这是另一个例子,看起来您的问题已经回答了。
Here is another example, looks like your question was already answered as I was writing this.
听起来您正在使用 Onchange 事件。
如果您尝试 onkeypress 它应该做自己的事。
Sounds like you are using the onchange event.
If you try onkeypress it should do what you are after.
您可以同时使用
输入
和更改
事件。当键按导致字符输入时,输入将发射。当字段变化后,文本字段的焦点丢失时,更改将发射,我认为在大多数浏览器中也从剪贴板粘贴到文本后。You could use both the
input
andchange
events. Input will fire when a key press results in a character input. Change will fire when the focus of the text field is lost after the value of the field has changed, and I think in most browsers also after text is pasted into it from the clipboard.