使用 jQuery 验证器确定输入的值不是数字

发布于 2024-11-10 15:39:23 字数 88 浏览 2 评论 0原文

我的问题很简单,但我找不到同样简单的解决方案。我有一个通过 jQuery 验证插件验证的文本字段,我想确保输入的值不是数字。最简单的方法是什么? 提前致谢! :)

My problem is very simple, but I can't find an equally simple solution. I have a text field that is validated via the jQuery validation plugin, and I want to make sure that the value entered is NOT a number. What is the simplest way to do so?
Thanks in advance! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-11-17 15:39:23

我还没有使用过 jQuery 验证器,但我想你可以提供自己的验证函数。

您可以这样做:

if(isNaN(+value)) {
    // not a number
}

编辑:显然,isNaN 也接受字符串,因此 isNaN(value) 就足够了

如果你想确保输入不包含任何数字(这与输入不是数字不同),那么你必须使用正则表达式:

if(!(/\d/.test(value))) {
    // does not contain digits
}

PS:无需 jQuery 也能工作;)

I didn't work with jQuery validator yet, but I guess you can provide your own validation function.

You could do:

if(isNaN(+value)) {
    // not a number
}

Edit: Apparently, isNaN accepts a string too, so isNaN(value) suffices.

If you want to ensure that the input does not contain any digit (which is different from the input not being a number), then you would have to use regular expressions:

if(!(/\d/.test(value))) {
    // does not contain digits
}

P.S.: Also works without jQuery ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文