如何使文本框只允许三位小数点后的数字?

发布于 2024-08-10 04:18:35 字数 109 浏览 8 评论 0原文

我想让文本框只允许三位数字和三位小数。例如 999.999。类似地,它不允许任何字符,如 a、b、/ 等。我该如何使用 Jquery ?

I want to make the textbox allow only three digits and three decimals.For example 999.999.similarly it doesnt allow any characters like a,b,/,etc.How can I do with the Jquery?

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

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

发布评论

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

评论(3

笑脸一如从前 2024-08-17 04:18:35

你看过 jQuery.validate 插件吗?

$('#formid').validate({
    rules: {
        fieldname: {
            required:function(element) {
                return /^\s*[0-9]{,3}(?:\.[0-9]{1,3})?\s*$/.test(element.value);
            }
        }
    }
});

编辑:诚然,我没有测试正则表达式,只是想演示它是如何工作的。
根据 nickf 的建议更新了正则表达式,谢谢。

Have you looked at the jQuery.validate plugin?

$('#formid').validate({
    rules: {
        fieldname: {
            required:function(element) {
                return /^\s*[0-9]{,3}(?:\.[0-9]{1,3})?\s*$/.test(element.value);
            }
        }
    }
});

EDIT: admittedly I didn't test the regex and merely wanted to demonstrate how it works.
Updated regex as per nickf's suggestion, thanks.

纵性 2024-08-17 04:18:35

看一下 jQuery 数字插件

Take a look at jQuery numeric plugin.

韶华倾负 2024-08-17 04:18:35

您还可以查看这个 屏蔽输入 插件

You could also look at this masked input plugin

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