10

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.

would I just add the ? at the end of the second half of the 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
  • Re edit: Well, what if they do? You're not telling us what you want to happen in that case.
    – chaos
    May 29, 2009 at 16:48

5 Answers 5

14

If what you want is for 1, 1.2, and 1.20 all to work:

/^(\d{1,3})(\.\d{1,2})?$/
1
  • OMG I'm so caught up in other stuff I didn't even think of this, LOL thanks May 29, 2009 at 17:18
2

Yes, just add a ? to the end of the second grouping to make it optional. That should work nicely.

0
0
$.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.

0

This was the best, holistic answer we've used for regex currency inputs that encompasses all we needed: /(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?(.[0-9]{1,2})?$/

0

I wanted to test on keyup. There fore I needed to accept 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.

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.

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...

1
  • Yes Drew. It did for my implementation. As others have pointed out, this does not fit into a localization related implementation. Mar 4, 2016 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.