jquery-maskmoney icon indicating copy to clipboard operation
jquery-maskmoney copied to clipboard

Mask not respected on Android

Open gbvaz opened this issue 9 years ago • 20 comments
trafficstars

Hi, I've been using this lib for quite a while now, it's pretty good, thank you for sharing it!

Recently I've noticed an odd behavior. On Android (that I know, 5.1 and lower ones), using Chrome (51.0.2704.81 and possibly other ones too), when I start typing numbers, say 1234 for example, in the field, I always get something like this $ 0.001234. Then, when I leave the field (blur), into another field it keeps the same $ 0.001234, but when I go back to it (focus), all of the sudden the mask is correctly applied $ 12.34.

Do you happen to have any Android device in order to simulate this situation? You can do so trying out the lib example page itself

Any help is appreciated!

Best wishes,

Guilherme Vaz

gbvaz avatar Aug 09 '16 00:08 gbvaz

I'm also having this issue!

amylashley avatar Aug 09 '16 14:08 amylashley

I'm having a similar issue.

When I type anything in an input using Android, nothing happens (it behaves like an ordinary input). When I blur it and then focus it again, the mask is suddenly applied to the current input (but won't be applied as I continue typing).

Model: Samsung Galaxy S4 OS: Android 4.4 Browser: Google Chrome 51.0.2704.81 Keyboard: SwiftKey 6.3.8.73

gutomotta avatar Aug 11 '16 19:08 gutomotta

@amylashley @GutoMotta since we got no solution for this, I've switched to this lib https://github.com/igorescobar/jQuery-Mask-Plugin it has a very similar syntax and it works great both on desktop/notebooks and mobile devices. Hope it helps!

gbvaz avatar Aug 17 '16 16:08 gbvaz

Try to replace keydownevent function with this one, sometimes keypress is not fired in the devices.

            function keydownEvent(e) {
                e = e || window.event;
                var key = e.which || e.charCode || e.keyCode,
                    keyPressedChar,
                    selection,
                    startPos,
                    endPos,
                    value,
                    lastNumber;
                //needed to handle an IE "special" event
                if (key === undefined) {
                    return false;
                }

                selection = getInputSelection();
                startPos = selection.start;
                endPos = selection.end;

                    if (key !== 8 && key !== 46 && key !== 63272) {
                        // any key except the numbers 0-9
                        if (key < 48 || (key > 57 && key < 96) || key > 105) {
                            // -(minus) key
                            if (key === 45) {
                                $input.val(changeSign());
                                return false;
                                // +(plus) key
                            } else if (key === 43) {
                                $input.val($input.val().replace("-", ""));
                                return false;
                                // enter key or tab key
                            } else if (key === 13 || key === 9) {
                                return true;
                            } else if ($.browser.mozilla && (key === 37 || key === 39) && e.charCode === 0) {
                                // needed for left arrow key or right arrow key with firefox
                                // the charCode part is to avoid allowing "%"(e.charCode 0, e.keyCode 37)
                                return true;
                            } else { // any other key with keycode less than 48 and greater than 57
                                preventDefault(e);
                                return -true;
                            }
                        } else if (!canInputMoreNumbers()) {
                            return false;
                        } else {
                            preventDefault(e);

                            if (key >= 96 && key <= 105) {
                                keyPressedChar = String.fromCharCode(key - 48);
                            }
                            else {
                                keyPressedChar = String.fromCharCode(key);
                            }

                            selection = getInputSelection();
                            startPos = selection.start;
                            endPos = selection.end;
                            value = $input.val();
                            $input.val(value.substring(0, startPos) + keyPressedChar + value.substring(endPos, value.length));
                            maskAndPosition(startPos + 1);
                            return false;
                        }
                    }


                if (key === 8 || key === 46 || key === 63272) { // backspace or delete key (with special case for safari)
                    preventDefault(e);

                    value = $input.val();
                    // not a selection
                    if (startPos === endPos) {
                        // backspace
                        if (key === 8) {
                            if (settings.suffix === "") {
                                startPos -= 1;
                            } else {
                                // needed to find the position of the last number to be erased
                                lastNumber = value.split("").reverse().join("").search(/\d/);
                                startPos = value.length - lastNumber - 1;
                                endPos = startPos + 1;
                            }
                            //delete
                        } else {
                            endPos += 1;
                        }
                    }

                    $input.val(value.substring(0, startPos) + value.substring(endPos, value.length));

                    maskAndPosition(startPos);
                    return false;
                } else if (key === 9) { // tab key
                    return true;
                } else { // any other key
                    return true;
                }
            }

kstyrose avatar Aug 31 '16 15:08 kstyrose

I tried to overwrite the keydownEvent() but it's not working yet (it behaves quite fuzzy, indeed). I am using Android 4.4 on a Samsung Tab3 8" tablet.

diebugger avatar Oct 13 '16 13:10 diebugger

Hi,

Also have same problem with...

Chrome 53.0.2785.124 or Internet 4.0.20-20 Android 6.0.1;SM-G903M Build/MMB29K

The https://github.com/BankFacil/vanilla-masker run correct, but don't have support to negative numbers and HTML5 data attributes.

Thanks

mbraz avatar Oct 25 '16 01:10 mbraz

I was able to solve it with this. It mimics the keypress function and I placed it right before the keydown function.

           $input.on('keyup', function (e) {
                e = e || window.event;
                var key = e.which || e.charCode || e.keyCode,
                    keyPressedChar,
                    selection,
                    startPos,
                    endPos,
                    value;
                selection = getInputSelection();
                startPos = selection.start;
                maskAndPosition(startPos + 1);
            });

abdulmhamid avatar Nov 08 '16 23:11 abdulmhamid

This solution worked!

goodeath avatar Mar 12 '17 01:03 goodeath

@aureliosaraiva can you help us with this?

clairton avatar Mar 29 '17 17:03 clairton

Tanks @gbvaz Works pretty good for me!

ggwebdev avatar Apr 15 '17 13:04 ggwebdev

@ggwebdev I'm glad I could help :)

gbvaz avatar Apr 18 '17 17:04 gbvaz

Thanks! This worked for me too!

alexnovelli avatar Jun 05 '18 20:06 alexnovelli

Other very simple workaround on this is just to set your input type as tel instead of text Eg: <input type="tel" inputmode="numeric"> 😉

evsar3 avatar Jan 11 '19 15:01 evsar3

FYI @abdulmhamid's solution requires you to update the source file. You can add the snippet on line 489: https://github.com/plentz/jquery-maskmoney/blob/master/src/jquery.maskMoney.js#L489

dreamawakening avatar Feb 14 '19 13:02 dreamawakening

I had issues with @abdulmhamid's code on browsers that weren't Chrome on Android.

A solution is to make the code only run on Chrome browsers for Android devices:

var ua = navigator.userAgent;
var isAndroid = /Android/i.test(ua);
var isChrome = /Chrome/i.test(ua);

// Fix masking on Chrome for mobile devices
if (isAndroid && isChrome) {
    $input.on('keyup', function(e) {
        e = e || window.event;
        var key = e.which || e.charCode || e.keyCode,
            keyPressedChar,
            selection,
            startPos,
            endPos,
            value;
        selection = getInputSelection();
        startPos = selection.start;
        maskAndPosition(startPos + 1);
    });
}

dreamawakening avatar Feb 20 '19 11:02 dreamawakening

Thanks @dspacejs ! This worked for me.

guipriolli avatar Jul 15 '19 14:07 guipriolli

I had too many HTML forms, so appyling @evsar3 solution via JQuery worked for me.

var ua = navigator.userAgent;
var isAndroid = /Android/i.test(ua);
var isChrome = /Chrome/i.test(ua);

// Fix masking on Chrome for mobile devices
if (isAndroid && isChrome) {
    $('.work_price').attr('type','tel');
}     

him-developer avatar Oct 12 '20 16:10 him-developer

Outra solução muito simples para isso é apenas definir seu tipo de entrada como, em telvez de text Ex: <input type="tel" inputmode="numeric"> piscadela

Working here! Thanks

filipevl avatar Oct 26 '21 13:10 filipevl

filipevl - agradeço resolveu meu problema

worldbet avatar Mar 29 '23 19:03 worldbet