html - 正则表达式仅在 html 输入框中允许字母和空格
问题描述
我想在输入框中只允许字母和空格,如“john deo”。但以上不适用于下面的正则表达式,并且不允许空间。
<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>
<label>full name
<input type="text" name="textfield" onkeydown="return /[a-z]/i.test(event.key)"
onblur="if (this.value == '') {this.value = '';}"
onfocus="if (this.value == '') {this.value = '';}"/>
</label>
</body>
</html>
解决方案
改变你的正则表达式/[a-z, ]/i
<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>
<label>full name
<input type="text" name="textfield" onkeydown="return /[a-z, ]/i.test(event.key)"
onblur="if (this.value == '') {this.value = '';}"
onfocus="if (this.value == '') {this.value = '';}"/>
</label>
</body>
</html>
推荐阅读
- c# - 如何在没有数据库的情况下使用cshtml通过viewmodel传递List
- javascript - 节点中更改时间的最佳方法是什么?
- tensorflow - 如何在 tensorflow 虚拟 GPU 上放置节点?
- gradle - 如何使用自定义 gradle 构建文件和 gradle 设置文件
- kdb - 在 kdb 中使用假日日历?
- java - IN主类不能调用子类
- ruby - 无法推送到 rubygems.org
- php - Laravel 应用程序在重定向时注销经过身份验证的用户(不应该)
- flutter - Flutter 存储应用通知的最佳方式?
- html - clipPath 被容器截断