jQuery-Mask-Plugin icon indicating copy to clipboard operation
jQuery-Mask-Plugin copied to clipboard

Mask bug in mobile

Open deivide opened this issue 11 years ago • 15 comments

As you type, the cursor changes alone position and bug the mask. For example: on a type 123 in input with mask 000.000.000,00 (reverse is true in this case), the number result is 13,2. The bug occurs when the smart text is enabled. If the reverse is enabled or not, the error occurs anyway.

The error was produced on a smartphone Samsung Galaxy S6 with android 5.0.2 Lollipop. If predictive text is disabled, the error not occurs.

deivide avatar Sep 30 '15 19:09 deivide

My colleagues have discovered a similar issue. Only two people can reproduce this. One has a Galaxy S6 with very latest Lollipop (5.1.1) and Chrome 45. The other has KitKat and latest Chrome on Tab 2. However, other devices with the same OS and Chrome version do not have this issue so it is very troubling to pinpoint. The two fields it happens on both are using a mask. One is a zip code which allows only five digits.You type the first digit, okay. Then you type the second digit and the cursor suddenly switches position and is now between the first and second digit. Type again, and the cursor again goes between the first and second digits. So if you were typing 1234, it would be 1432. Something similar is happening in Birthdate field. URL = www.connecticare.com/SOLO/Quoting/ then click Continue to go to the second page. This definitely wasn't happening a few weeks ago. Thanks

Update, this appears to only happen with a Samsung Keyboard and Predictive Text turned on. http://www.intellectdigest.in/disable-predictive-text-input-in-android-keyboard-4692/

njburkard avatar Oct 02 '15 11:10 njburkard

Third times a charm... running into this now. Samsung S4 with predictive type.

After going through the code in the /src. The issue is happening on line 103: ctrl.setSelectionRange(pos,pos);

It looks like a Samsung device is inaccurately triggering the conditional and setting the start and end points of the selection in the middle of the input field value. I can either comment out this line or disable the if:else statement so that the logic that calculates the createTextRange is triggered instead.

I'm opting for that functionality at the moment, but I hope this helps you debug further and compensate accordingly.

Code starting at line 102: Before if (ctrl.setSelectionRange) { ctrl.setSelectionRange(pos,pos); } else if (ctrl.createTextRange) { range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); }

After range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select();

Hope that helps others in the meantime.

uiuxdeveloper avatar Oct 12 '15 23:10 uiuxdeveloper

I got the exact same problem on my Samsung Galaxy S4 Mini. (Works perfect on desktop)

@uiuxdeveloper Thank you very much, this solves the problem for my android device. Lets see if it also works on iPhones.

RyxMedia avatar Oct 23 '15 19:10 RyxMedia

I had the similar issue on Samsung S5 and S6. If I entered 12345, it would enter 15432. @uiuxdeveloper solutions fixes this for me. Thanks a bunch.

SandyRig avatar Oct 28 '15 22:10 SandyRig

I also have the same problem with my Samsung S5 only with Chrome. After typing the second character the caret jumps in front of the second character, resulting to reversed input. ex. postal 1234 becomes 1432.

the code used:

$('#postal').mask('9000 ZZ', {
        "placeholder": "____ __",
        translation: {
            '9': {
                pattern: /[1-9]/,
                optional: false
            },
            'Z': {
                pattern: /[a-zA-Z]/,
                optional: false
            }
        }
    });

chameleonlizzard avatar Nov 03 '15 13:11 chameleonlizzard

Had the problem with S5 Chrome, it works now thanks to @uiuxdeveloper

erdimeola avatar Nov 09 '15 08:11 erdimeola

Android has both methods defined ctrl.setSelectionRange and ctrl.createTextRange ?

igorescobar avatar Dec 04 '15 17:12 igorescobar

Any update on merging this in ?

lukepolo avatar Jan 29 '16 16:01 lukepolo

I have one more solution worked for me in behaviour-method:

behaviour: function(e) {
                e = e || window.event;
                p.invalid = [];
                var keyCode = e.keyCode || e.which;
                if ($.inArray(keyCode, jMask.byPassKeys) === -1) {

                    var tryChangeCarret = function() {
                        var caretPos = p.getCaret(),
                            currVal = p.val(),
                            currValL = currVal.length,
                            changeCaret = caretPos < currValL,
                            newVal = p.getMasked(),
                            newValL = newVal.length,
                            maskDif = p.getMCharsBeforeCount(newValL - 1) - p.getMCharsBeforeCount(currValL - 1);

                        p.val(newVal);

                        // change caret but avoid CTRL+A
                        if (changeCaret && !(keyCode === 65 && e.ctrlKey)) {
                            // Avoid adjusting caret on backspace or delete
                            if (!(keyCode === 8 || keyCode === 46)) {
                                caretPos = p.caretPos(caretPos, currValL, newValL, maskDif);
                            }
                            p.setCaret(caretPos);
                        }
                    };

                    if (navigator.userAgent.indexOf("Android") > -1) {
                        setTimeout(tryChangeCarret, 0);
                    } else {
                        tryChangeCarret();
                    }

                    return p.callbacks(e);
                }
            },

I hope it can be useful.

gregory-hasyn avatar Jan 29 '16 16:01 gregory-hasyn

The solution from @gregory-hasyn worked as charm.

glaucomorais avatar Feb 25 '16 19:02 glaucomorais

Fixed :)

igorescobar avatar Mar 06 '16 22:03 igorescobar

hello @igorescobar thank for this plug , i tried your new corrections , but not fixed. @gregory-hasyn solution fixe this issue thank all guys tried on sony xperia android 5.0

br750 avatar Apr 18 '16 13:04 br750

This issue's commit not applied to latest 'master' branch. https://github.com/igorescobar/jQuery-Mask-Plugin/commit/050a876b9c94796cbd422092b980ae0003d6c243

erayaydin avatar Jun 13 '16 14:06 erayaydin

Hi, the Chrome running on Samsung A7 have a different behavior than other browsers on some functions.

After type one character on a input, all other browsers return "1" when function selectionStart is called. Samsung A7 returns "0". After the second character is typed other browsers returns "2" and A7 returns "1" and so on.

Another unexpected behavior occurs when setSelectionRange function is called. This function has two first parameters "start position" and " final position". If is passed the same number on both parameters, all browsers understand a "empty" selection and just adjust the caret position to given position. Samsung A7 select the first character before the position passed.

Here is my solution:

// Firefox, WebKit, etc..
if (ctrl.setSelectionRange && navigator.appVersion.indexOf('SM-A7') === -1) {
    ctrl.focus();
    ctrl.setSelectionRange(pos, pos);
} else { // IE
    range = ctrl.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
}

ebanolopes avatar Jun 17 '16 04:06 ebanolopes

I had a cursor bug in Samsung S5 . @uiuxdeveloper solutions fixes this for me. However it was giving issue in browser when I have put this code : So , I have used the following code, var ua = navigator.userAgent.toLowerCase(); var isAndroid = ua.indexOf("android 4.4.2") > -1; // samsung s5 console.log(isAndroid); ....

setCaret: function(pos) { try { if (el.is(':focus')) { var range, ctrl = el.get(0); // Firefox, WebKit, etc.. if (ctrl.setSelectionRange && !isAndroid) { // added this condition ctrl.setSelectionRange(pos, pos); } else { // IE range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } } } catch (e) {} },

Hope this will be of some use.

basanthi avatar Jul 20 '18 07:07 basanthi