Mask Money Jquery plug in

user171523 picture user171523 · May 2, 2011 · Viewed 8.6k times · Source

I am using below Jquery plug in. I am able to mask the values. On blur event i need to perform some validations like. Take two text box values and calulate them. There is no way we can un mask the value or conver into to float. http://github.com/plentz/jquery-maskmoney

Answer

Andrew Whitaker picture Andrew Whitaker · May 3, 2011

You're going to have to provide much more detail and probably some code to receive a good answer, but here's how you could strip out only the decimal value of the masked input on blur (regular expressions are your friend):

function getFloat(text) {
    var regex = /\d+|\./g,
        matches,
        num = "";

    while(matches = regex.exec(text)) {
        num += matches[0];
    }
    return parseFloat(num) || 0;
}

$("#price1, #price2")
    .maskMoney({ showSymbol: true })
    .bind("blur", function() {
        var price1 = getFloat($("#price1").val());
        var price2 = getFloat($("#price2").val());

        $("#total").text((price1 + price2).toFixed(2));
    });

Here's a working example: http://jsfiddle.net/andrewwhitaker/enJEe/

Again, providing some more detail about your problem will increase the odds of your question being answered quickly and correctly.