javascript - 如何在输入文本字段中只允许字符串
问题描述
如何使用 rails haml 表单仅允许在 text_field 上输入字符串。
.field
= f.label "agent_name"
= f.text_field :agent_name, :required => true,:id=>"agent_name_validation"
$("#agent_name_validation").keypress(function(event) {
var string = /^[a-z]+$/i;
if(event.which != string){
return false;
}
});
解决方案
使用以下 Jquery 函数从文本字段中删除数字
$("#agent_name_validation").keyup(function(e) {
// Our regex
// a-z => allow all lowercase alphabets
// A-Z => allow all uppercase alphabets
var regex = /^[a-zA-Z]+$/;
// This is will test the value against the regex
// Will return True if regex satisfied
if (regex.test(this.value) !== true)
//alert if not true
//alert("Invalid Input");
// You can replace the invalid characters by:
this.value = this.value.replace(/[^a-zA-Z]+/, '');
});
推荐阅读
- sql-server - 如何在 SSIS 中从两个源追加两行?
- c# - 先用点表示小数,然后用逗号表示
- pandas - 带有权重的熊猫样本
- java - 无法解析方法 getElevation()
- linux-kernel - 无法在 Linux 驱动程序中请求 Raspberry Pi 上的键盘 IRQ 1
- javascript - 如何使用 beforeunload 事件使用 html2canvas 截取页面
- python-3.x - 通过从列表中获取列名来乘以数据框的列
- react-native - React Native Expo 错误 500 与 expo/hashAssetFiles.js 相关。Bundler 卡在构建 JS Bundle
- java - 具有长类名和字段名的 Java 默认序列化
- react-native - React Native:react-native run-android 给出错误