jQuery 如何在jquery中验证金钱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21017867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:23:37  来源:igfitidea点击:

how to validate for money in jquery

jqueryvalidation

提问by Santosh

how to validate for money in jquery where max amount i can enter is 9999.99 so my requirements are: 1.it should allow only digits and one dot 2.max length is 7(including dot) 3.before dot max length is 4 4.after dot max length is 2

如何在 jquery 中验证钱,我可以输入的最大金额是 9999.99 所以我的要求是:1.它应该只允许数字和一个点 2.最大长度为 7(包括点) 3.before 点最大长度为 4 4。点最大长度为 2 后

currently my textbox allows only digits and one dot with the following code

目前我的文本框只允许使用以下代码的数字和一个点

$('#txtpaymentamnt, #txttenderamt').on("keypress", function (e) {
    alert(e.which);
    if (e.which != 8 && e.which != 0 && ((e.which < 48 || e.which > 57) && e.which != 46)) {
        e.preventDefault();
    }

回答by James Hibbard

You can do this with a regular expression: /^\d{0,4}(\.\d{0,2})?$/

您可以使用正则表达式执行此操作: /^\d{0,4}(\.\d{0,2})?$/

EDIT:

编辑:

Here's the requested fiddle

这是要求的小提琴

回答by Moni

With jQuery validation pluginyou can set your own rule using Hyman's regular expression (which I haven't tested though) like this:

使用jQuery 验证插件,您可以使用 Hyman 的正则表达式(虽然我没有测试过)设置自己的规则,如下所示:

jQuery.validator.addMethod(
    "money",
    function(value, element) {
        var isValidMoney = /^\d{0,4}(\.\d{0,2})?$/.test(value);
        return this.optional(element) || isValidMoney;
    },
    "Insert "
);

Then you can set the rule within using the validate method:

然后你可以使用 validate 方法设置规则:

$("#myForm").validate({
rules: {
    nameOfMyInputField: {
        money: true,
    }
}
});

回答by Nitin Varpe

As you said, if you are using MVC. MVCframework provides annotationswhich can be used to best effects. Even if there is no explicit data annotation for a decimal.

正如你所说,如果你正在使用MVC. MVC框架提供了annotations可以使用的最佳效果。即使 a 没有明确的数据注释decimal

Following can be used for Two Decimal places

以下可用于两位小数位

[RegularExpression(@"^\d+.\d{0,2}$")]

And for range you can use

对于范围,您可以使用

[Range(0, 9999.99)]

So your model field becomes

所以你的模型字段变成

[RegularExpression(@"^\d+.\d{0,2}$")]
[Range(0, 9999.99)]
public decimal Money{ get; set; }

Hope It Helps

希望能帮助到你

回答by pauix

If I were you, I'd use the isNumeric function in this answerto check if the input is a number and then check if the number is valid for your case.

如果我是你,我会在这个答案中使用isNumeric 函数来检查输入是否为数字,然后检查该数字是否对您的情况有效。

To ve a "valid number" (according to your requirements), it has to:

要获得“有效号码”(根据您的要求),它必须:

  • Be between 0 and 9999.99 (maximum of 4 digits before the dot).

  • Have a maximum of 2 decimal digits. A good way to check that is to truncate the number after the nth digit (in your case, the second one) and compare it to the original value(two different values mean the second number has more decimal digits than the first). Use Math.round(number*100)/100to truncate everything after the second decimal digit.

  • 介于 0 和 9999.99 之间(点前最多 4 位数字)。

  • 最多有 2 个十进制数字。检查这一点的一个好方法是截断第 n 个数字(在您的情况下,第二个数字)之后的数字并将其与原始值进行比较(两个不同的值意味着第二个数字的十进制数字比第一个数字多)。使用Math.round(number*100)/100到第二位小数点后截断的一切。

I hope this helps you with your problems.

我希望这可以帮助您解决问题。

回答by Purvi Barot

$.validator.addMethod("currency", function (value, element) {   return
this.optional(element) ||
/^$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(value); }, "Please
specify a valid amount");

here is the jquery validation function.

这是 jquery 验证功能。