简体   繁体   English

用于文本框验证的HTML5正则表达式模式:仅允许字母,空格和连字符

[英]HTML5 Regex Pattern for textbox validation: allow alphabet, spaces, and hyphens only

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. 我正在用html创建一个表单,对于我的名字和姓氏文本输入,我想对它们进行正则表达式验证,该验证只允许用户输入:字母,空格和连字符。

I have tried this pattern but to no avail: 我尝试过这种模式,但无济于事:

[A-Za-z -]

My HTML code: 我的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 -]+$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM