masked input not working in android mobiles?

后端 未结 8 1182
一生所求
一生所求 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条回答
  • 2020-12-14 15:57

    I tried using the raw script that Jonathan Rowny mentioned, but I was still having the same problem on an S3 - Chrome browser. I think it has something to do with inputs type="tel" and/or type="number". I ended up having to use another plugin. http://igorescobar.github.io/jQuery-Mask-Plugin/

    jquery-mask (not to be confused with jquery-masked-input) is very similar but the syntax was slightly different. Hope this helps anyone else with this issue.

    jquery-masked-input syntax: $("#phone").mask("(999) 999-9999");

    VS

    jquery-mask syntax: ('#phone').mask('(000) 000-0000');

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题