masked input not working in android mobiles?

后端 未结 8 1196
一生所求
一生所求 2020-12-14 15:17

I am using the digitalbush masked input jQuery plugin. It is working fine in web browsers and the iPhone browser perfectly, but it is not working for Android mobile devices.

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 16:02

    To further enhance the accepted answer of Tony Brasunas, add following snippet in jquery.maskedinput.js for point number 3 to dynamically increase maxlength so it doesn't interfere with caret action.

    Remove the input's maxlength attribute or set it to a value certain not to interfere with the caret action, like 20.

        defs = $.mask.definitions;
        tests = [];
        partialPosition = len = mask.length;
        firstNonMaskPos = null;
    
        //insert snippet below
        if (chrome && android) {
            console.log("chrome && android");
            var allAllowedRegExps = '';
            jQuery.each(defs, function (key, value) {
                allAllowedRegExps = allAllowedRegExps + key;
            });
            allAllowedRegExps = allAllowedRegExps.replace(/\[/g, '');
            allAllowedRegExps = allAllowedRegExps.replace(/\]/g, '');
            allAllowedRegExps = '[^' + allAllowedRegExps + ']';
            var re = new RegExp(allAllowedRegExps, "g");
            var actual = mask;
            var replacedVal = actual.replace(re, "");
            var actualValue = actual.length - replacedVal.length;
            if ($(this).attr('maxlength') !== undefined) {
                $(this).attr('maxlength', parseInt(this.attr('maxlength')) + parseInt(actualValue));
            }
       }
    
        mask = String(mask);
    

提交回复
热议问题