HTML输入不允​​许数字 [英] HTML input do not allow numbers

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

问题描述

现在我有一个像这样的输入字段:

Right now I have an input field like this:

<input class="form-control" type="text"/>

但是它仍然允许输入数字.

But it stills allows the input of numbers.

我想要输入名称,并且想要在字符串包含数字时显示错误消息.我该如何实现?

I want names to be input and want to display an error message when the string contains a number. How can I achieve this?

推荐答案

您可以使用本机 HTML5字段验证

类似电子邮件验证(小提琴): <input type="text" title="email" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />

like e-mail validation (fiddle): <input type="text" title="email" pattern="[^@]+@[^@]+\.[a-zA-Z]{2,6}" />

对于您的具体情况,您可以使用pattern="[a-zA-Z]*"之类的正则表达式( fiddle )

For your specific case you can use regexp like pattern="[a-zA-Z]*" (fiddle)

提交表单时,表单会以红色边框突出显示,以向您显示验证错误.在不同的浏览器中,它的行为会略有不同.

When you submit form it became highlighted with red border to show you validation error. In different browser it will behave slightly different.

我认为没有覆盖所有默认样式的标准方法,但是存在特定于浏览器的方法(

I don't think there is standard way to override every default styling, however there are browser specific ways to do this (here).

对于某些样式,您可以使用CSS钩子进行更改请参见此处

For some style you can have css hooks in place for changes see here

更新了小提琴.

这篇关于HTML输入不允​​许数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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