Mask bug in mobile
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.
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/
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.
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.
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.
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
}
}
});
Had the problem with S5 Chrome, it works now thanks to @uiuxdeveloper
Android has both methods defined ctrl.setSelectionRange and ctrl.createTextRange ?
Any update on merging this in ?
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.
The solution from @gregory-hasyn worked as charm.
Fixed :)
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
This issue's commit not applied to latest 'master' branch. https://github.com/igorescobar/jQuery-Mask-Plugin/commit/050a876b9c94796cbd422092b980ae0003d6c243
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();
}
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.