question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `decimalCharacterAlternative` character is not translated to the `decimalCharacter` on paste

See original GitHub issue

Expected behavior

When pasting a number with the alternate decimal character, I expect that it be treated the same as the primary decimal character. I.e. if decimalCharacterAlternative is & (ampersand), when pasting 123&45, I expect to see 123.45 in the input field.

Actual behavior

The ampersand is ignored and the number is treated as an integer. I.e. when pasting 123&45 the field is set to 12345.00. The behavior is the same regardless of what character is used as alternative.

Steps to reproduce the problem

  1. Using autoNumeric 2.0.10 and the Chrome version 56 on MacOS Sierra, and also Safari 10.0.3.
  2. I used the options
{
  decimalCharacterAlternative: '&' 
}
  1. Initialized my AutoNumeric object with the call
$('input').autoNumeric('init', {
  decimalCharacterAlternative: '&',
});
  1. I copy “123&45” to the clipboard and paste it into the field. I get 12345,00.

Link to live example

https://jsfiddle.net/matssigge/fwo5tn70/

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
VoimiXcommented, Oct 22, 2017

Does somebody know how to fix the issue “45,34 -> 4534.00” without modifying the autonumeric code? Maybe there is a callback function where I can write my own custom logic after paste? Is there any workaround?

In my country people copy values from Excel (to the web app). These values can be in the following format “3 422 234,00” -Only spaces as group delimeters. Decimal delimeter can be “,” or “.” In my case I dont care about hypothetical values, such as 1,234,567.89

1reaction
vecsscommented, Sep 18, 2018

Just want to share my cross-browser solution (not too clean code, but it works. You could refine it according to your own taste).

new AutoNumeric('#sum', {
	digitGroupSeparator	: ' ',
	decimalCharacter	: '.',
	decimalCharacterAlternative: ','
});
SetupPasteNumberReformatter($('#sum'));

SetupPasteNumberReformatter function:

function SetupPasteNumberReformatter(jqEl) {
    if (jqEl && jqEl.length > 0) {
        jqEl.bind('paste', function(e) {
            e.preventDefault();
            var clipboardData = (e.originalEvent || e).clipboardData || window.clipboardData;
            var pastedText    = undefined;
            if (window.clipboardData && window.clipboardData.getData) { // IE
                pastedText = window.clipboardData.getData('Text');
            } else {
                if (clipboardData && clipboardData.getData) {
                    pastedText = clipboardData.getData('text/plain');
                }
            }
            if (pastedText != undefined && pastedText.indexOf(',') !== -1) {
                pastedText    = pastedText.replace(',', '.');
                var triggerAn = AutoNumeric.getAutoNumericElement(jqEl[0]);
                triggerAn.set(pastedText);
                return false;
            }
        });
    };
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

autoNumeric won't allow decimal input from numeric keyboard ...
Use "decimalCharacterAlternative": "." which will work as alternative for your main decimal character when pressed. Here is more options.
Read more >
autonumeric - UNPKG
n\t * - 'truncate' : autoNumeric will insert as many pasted numbers it can at ... tries to paste a single decimal character...
Read more >
Easy Number and Currency Formatting Library - autoNumeric
/* Defines if the decimal character or decimal character alternative should be accepted when there is already a decimal character shown in the ......
Read more >
Restrict copy, paste and special characters - OutSystems
ready function. - Reference your textbox runtime id. (this will be translated automaticly to the html id of the element). - Disable the...
Read more >
char conversion when copy+paste into a text field — oracle-tech
various characters in the source text(word document) which are not translating well when copy+pasted. For example: The source text has a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found