Phill Pafford
Phill Pafford

Reputation: 85378

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.

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 ?

Upvotes: 10

Views: 17621

Answers (5)

Spencer Sullivan
Spencer Sullivan

Reputation: 575

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

Upvotes: 0

Opi Hana
Opi Hana

Reputation: 133

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})?$/

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

Upvotes: 0

chaos
chaos

Reputation: 124365

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

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

Upvotes: 14

James
James

Reputation: 12806

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

Upvotes: 2

Related Questions