fabiomcosta / jquery-meiomask

A jquery plugin for creating masked input texts
http://meiocodigo.com/projects/meiomask
MIT License
233 stars 105 forks source link

carret displaced on ie11 and chrome #60

Open cavez86 opened 10 years ago

cavez86 commented 10 years ago

On chrome and ie11 the cursor is moved tho the end of the field every key you press. msie check in fact doesn't match with ie11, bou mozilla does.

It happens in _onPaste function called by _onKeyUp, here:

//fix so ie's and safari's caret won't go to the end of the input value. if (($.browser.msie || $.browser.safari) && !o.reverse) this.__setRange(o._this, o.range.start, o.range.end);

tiagoperesmobilecard commented 10 years ago

1st: correct the value of the variable $.browser. The following works for jquery 1.9+:

if (!$.browser) {
    var uaMatch = function(ua) {
        ua = ua.toLowerCase();

        var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || /(webkit)[ \/]([\w.]+)/.exec(ua) || /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || /(msie) ([\w.]+)/.exec(ua) || ua.indexOf('compatible') < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || [];

        return match[2] || '0';
    };

    var isIE11 = function () { return ((navigator.appName == 'Microsoft Internet Explorer') || ((navigator.appName == 'Netscape') && (new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})").exec(navigator.userAgent) != null))); };

    $.browser = {
        mozilla: /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase()),
        webkit: /webkit/.test(navigator.userAgent.toLowerCase()),
        opera: /opera/.test(navigator.userAgent.toLowerCase()),
        msie: /msie/.test(navigator.userAgent.toLowerCase()) || isIE11(),
        android: (navigator.userAgent.toLowerCase().indexOf('mozilla/5.0') > -1 && navigator.userAgent.toLowerCase().indexOf('android ') > -1 && navigator.userAgent.toLowerCase().indexOf('applewebkit') > -1),
        version: uaMatch(navigator.userAgent)
    };
}

2nd: In "__getRange", make the following changes:

        __getRange: function(input) {
            if ((!$.browser.msie || $.browser.msie && $.browser.version >= 11) && !$.browser.android) return {
                start: input.selectionStart,
                end: input.selectionEnd
            };
nikolasmagno commented 9 years ago

Hello,

tiagoperesmobilecard I tried you solution and doe's not work, can you make a fork or somethink like that?