HTML5限制输入字符 [英] HTML5 restricting input characaters

查看:421
本文介绍了HTML5限制输入字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以限制HTML5 / JavaScript中某些字符的输入?例如,我可以在屏幕上有一个输入文本框,如果用户试图输入一个字母,它就不会显示在框中,因为我将其限制为只有数字?

Is it possible to restrict the input of certain characters in HTML5/JavaScript? For example could I have a input textbox on the screen and if the user tries to type a letter it wouldn't show up in the box because I've restricted it to only numbers?

我知道你可以使用会检查提交的模式,但是我希望根本不输入坏字符。

I know you can use pattern which will check on submit, but I want the "bad" characters to just never be entered at all.

推荐答案

使用输入的html5模式属性:

Use html5 pattern attribute for inputs:

<input type="text" pattern="\d*" title="Only digits" />

OR

使用html5数字类型输入:

Use html5 number type for input :

<input type="number" />

这篇关于HTML5限制输入字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆