javascript - 如何允许除数字以外的所有字符?
问题描述
我想允许除数字之外的所有字符。我在jquery下面写了。
$(".no-numbers").keypress(function(e){
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
{
swal({
title: "",
text: "Numbers are not allowed"
});
return false;
}
else
{
return true;
}
});
上面的代码不接受任何东西......请帮忙!!!
解决方案
尝试以下操作:
$(".no-numbers").keypress(function(e){
var key = e.keyCode;
if (key >= 48 && key <= 57) {
swal({
title: "",
text: "Numbers are not allowed"
});
return false;
}
else
{
return true;
}
});
或者您可以使用正则表达式,例如:
var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
// do your stuff here
}
推荐阅读
- go - 下载 Go 包
- angular - 在 Spring Boot 中将 Angular 中的 put 请求传递给后端时出现错误 400
- javascript - 从 ajax 响应中获取表列平均值
- ruby-on-rails - 模型的 rails 单元测试返回无效结果
- python - Python问题:如何解决while函数中的简单数字?
- playwright - Playwright 是否在命令行中提供自定义参数?
- java - React Native CLI 0.64 版设置任务':app:checkDebugDuplicateClasses'的执行失败。和任务:app:compileDebugJavaWithJavac FAILED
- python - 如果值重复,如何从字典中删除重复项
- excel - Excel VBA - 将一行中的单元格复制到非错误值
- java - android中的ViewModel和可变实时数据无法正常工作