简体   繁体   English

jQuery US货币验证regEx也允许整数

[英]jQuery US Currency validation regEx to allow whole numbers as well

I have this regex working but now need to allow numbers without the decimal as well 我有这个正则表达式,但现在需要允许没有小数的数字

// Validate for 2 decimal for money
jQuery.validator.addMethod("decimalTwo", function(value, element) {
    return this.optional(element) || /^(\d{1,3})(\.\d{2})$/.test(value);
}, "Must be in US currency format 0.99");

Currently this forces the user to at least have the .00 added to a number, would like it to allow both the current regex and whole numbers without the decimal. 目前这迫使用户至少将.00添加到一个数字,希望它允许当前正则表达式和没有小数的整数。

would I just add the ? 我会加上吗? at the end of the second half of the RegEx? 在RegEx的下半年结束?

// Validate for 2 decimal for money
jQuery.validator.addMethod("decimalTwo", function(value, element) {
    return this.optional(element) || /^(\d{1,3})(\.\d{2})?$/.test(value);
}, "Must be in US currency format 0.99");

EDIT: 编辑:

Ok but what if someone enters 1.2 ? 好吧,但如果有人输入1.2怎么办?

如果您想要的是1,1.2和1.20全部工作:

/^(\d{1,3})(\.\d{1,2})?$/

Yes, just add a ? 是的,只需添加一个? to the end of the second grouping to make it optional. 到第二个分组的结尾,使其成为可选的。 That should work nicely. 这应该很好。

$.validator.addMethod("money", function (value, element) {
     if ($("#<%= rdOtherAmt.ClientID %> input:checked")) {
         return this.optional(element) || /^((\d{1,5})+\.\d{2})?$|^\$?[\.]([\d][\d]?)$/.test(value);
     }                 
});

This will work perfectly, takes two decimals. 这将完美地工作,需要两位小数。

这是我们用于正则表达式货币输入的最佳,全面的答案,包括我们所需的全部:/(?=。)^ \\ $?(([1-9] [0-9] {0,2}(, [0-9] {3})*)|??[0-9] +)([0-9] {1,2})$ /

I wanted to test on keyup. 我想测试一下keyup。 There fore I needed to accept 999,999. 因此我需要接受999,999。 before the first decimal position (tenths) was entered. 在输入第一个小数位(十分之一)之前。 But, I still wanted to max at 2 decimal (hundredths) positions - assuming entered 999.1 == 999.10 in my code. 但是,我仍然想要最多2位小数(百分位) - 假设在我的代码中输入了999.1 == 999.10。

var currencywithcommas = /^(\d+|\d{1,3}(,\d{3})*)+(\.\d{2})?$/;
if ($("#yourelement").val().match(currencywithcommas)) {
  // positive match code
}

UPDATE: I ended up using jQuery Validation Plugin due the the implementation. 更新:由于实现,我最终使用了jQuery Validation Plugin。

Prototype code: 原型代码:

jQuery.validator.addMethod("currencywithcommas", function (value, element) {
    var currencywithcommasReg = /^(\d+|\d{1,3}(,\d{3})*)+(\.\d{2})?$/;
    return this.optional(element) || value.match(currencywithcommasReg);
}, "Must be in US currency format 9.99");

Implementation: 执行:

    $("#form1").validate({
        rules: {
            amount1: {
                required: true,
                currencywithcommas: true
            },
...

Same concept, but extended to use the validate functionality. 相同的概念,但扩展到使用验证功能。

$(".reqField1").each(function () {
    $(this).keyup(function () {
        $("#submitButton").prop("disabled", CheckInputs(".reqField1"));
    });
});

function CheckInputs(reqfldclass) {
    var valid = false;
    $(".reqField1").each(function () {
        if (valid) { return valid; }
        var input = $.trim($(this).val());
        valid = !input;
    });
    return valid;
}

And, Bob's your uncle... 而且,鲍勃是你的叔叔......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM