Masking double removes decimal point in back end

Masking double removes decimal point in back end

adam.owocyycadam.owocyyc Posts: 1Questions: 1Answers: 0

I am using https://editor.datatables.net/plug-ins/field-type/editor.mask to force a '$' character and numbers only in my field with an editor popup (MVC Core). It always returns **without **a decimal point even though the field is a double and the UI is showing a decimal point. I'm not sure is this an issue with datatables, or the jQueryMask plugin.

I have tried both of the following masks

                 {
                    label: "Tax:",
                    name: "taxAmount",
                    type: "mask",
                    mask: "$99999999999.00"
                },
                    {
                    label: "Total:",
                    name: "totalAmount",
                    type: "mask",
                    mask: "$#,##0.00",
                    maskOptions: {
                        reverse: true
                    }
        },

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Can you give me a link to your page showing the issue please? I'd like to see what data is being submitted to the server when creating / editing a row, and also what is being returned from the server.

    Thanks,
    Allan

  • jhegeduisjhegeduis Posts: 2Questions: 1Answers: 0
    edited September 2021

    Was this ever resolved? I am having a similar issue.

    I am trying to save 1234.23 to the backend but it's sent as an integer. Here's the relevant part of the payload:

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Our accounts aren't showing that you have an Editor license - it just reports that your organisation's trial expired several years ago. Is the license registered to another email address? Please can let us know so we can update our records and provide support.

    Thanks,

    Colin

  • tech@shefield.comtech@shefield.com Posts: 3Questions: 0Answers: 0

    Hi, could someone please look into this. I've also encountered the same issue as reported above.

    This is what I have for the Editor field configuration on the client:
    var editor = (new $.fn.dataTable.Editor({ ajax: ajaxURL, table: tableID, fields: [{ type: 'mask', mask: '#,##0.##', maskOptions: { reverse: true, placeholder: '', }, label: 'Bundle Price', name: 'Promotion.BundlePrice', },

    The masked edit field appears to work correctly on the add and edit Editor modal forms. However, if I enter 123.45, the request payload sent to the server looks like this:

    data[row_148300][Promotion][BundlePrice]: 12345

    So, 12345 (no decimal point) gets inserted into the database instead of 123.45. The next time I load the page, DataTables and the Editor forms show 12345.00

    In the Chrome developer tools console, if I enter $('#DTE_Field_Promotion-BundlePrice').val();, it returns '123.45', so I think the problem is in DataTables and not the jQuery masked input plugin.

  • tech@shefield.comtech@shefield.com Posts: 3Questions: 0Answers: 0

    The problem appears to be in the recommended JavaScript we're to include, as show on this page of the documentation:

    https://editor.datatables.net/plug-ins/field-type/editor.mask

    get: function (conf) { return conf._input.cleanVal(); },

    The cleanVal() function returns the user's input with the decimal point stripped away. I'm not sure where the cleanVal function resides or how it's supposed to work, but it seems like removing the decimal point is at odds with the idea of using a masked input in the first place.

  • tech@shefield.comtech@shefield.com Posts: 3Questions: 0Answers: 0

    I believe I found the solution. Replacing cleanVal() with val() returns the correctly formatted value.
    // return conf._input.cleanVal(); return conf._input.val();
    Are there any downsides to using val() instead?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    cleanVal() is a method of the Mask library.

    I think the only downside with using val() instead would be that you might get the masking characters - but numeric validation would catch that.

    Given that it goes as far as stripping decimal points out, I reckon I'll change the plug-in to using val().

    Thanks,
    Allan

Sign In or Register to comment.