用于文本框验证的HTML5正则表达式模式:仅允许使用字母,空格和连字符 [英] HTML5 Regex Pattern for textbox validation: allow alphabet, spaces, and hyphens only

查看:151
本文介绍了用于文本框验证的HTML5正则表达式模式:仅允许使用字母,空格和连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用html创建一个表单,对于我的名字和姓氏文本输入,我想对它们进行正则表达式验证,仅允许用户输入:字母,空格和连字符.

I am creating a form in html and for my first and last name text inputs, I want to have a regex validation on them that only allows the user to input: letters of the alphabet, spaces, and hyphens.

我尝试过这种模式,但无济于事:

I have tried this pattern but to no avail:

[A-Za-z -]

我的HTML代码:

<input type="text" id="first_name" name="first_name" maxlength="25" required="required" pattern="[A-Za-z -]"/>

我知道如何仅获取字母字符,但是我只能管理空格和连字符.

I know how to get only alphabet characters, but allowing spaces and hyphens only is something I'm unable to manage.

推荐答案

添加锚点和量词:

^[A-Za-z -]+$

这篇关于用于文本框验证的HTML5正则表达式模式:仅允许使用字母,空格和连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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