Search code examples
javascriptjqueryasp.net-mvcautonumber

Latest Autonumeric is allowing decimal point even if decimalPlaces set to 0


I have requirement to allow whole numbers for the input textboxes, I do have the requirement to have currency symbol along with commas. I have used the latest version of autonumeric js.

I have set the decimalPlaces property to 0 but still it is allowing me press the dot once and upon further keypress dot is being removed. I want make the dot not be pressed at the first place if the decimalPlace property is set to 0

below is the snnipet along with JS fiddle link. Looking for any sort of help on this

AutoNumeric.multiple('.testInput', 
{ currencySymbol: '$', decimalPlaces: 0, unformatOnSubmit: true, modifyValueOnWheel: false });

http://jsfiddle.net/wpomn0d2/


Solution

  • You could get the decimalPlaces value by using getPredefinedOptions() function and specify the integer.decimalPlaces property:

    var places = AutoNumeric.getPredefinedOptions().integer.decimalPlaces;
    

    Then use its value to prevent non-numeric symbols inserted on keydown event:

    $('.testInput').keydown(function (e) {
        var places = AutoNumeric.getPredefinedOptions().integer.decimalPlaces;
    
        if (places == 0 && (e.which < 48 || e.which > 57))
        {
            // prevent symbol insertion
            return false;
        }
    
        // other stuff
    });
    

    JSFiddle example

    Reference: AutoNumeric static methods