T
T
Tutucu2017-11-13 18:24:25
iPhone
Tutucu, 2017-11-13 18:24:25

Why does inputmask on IPhone work crookedly?

Hello, there is a mask for the phone, everything works fine on the PC, on Android and IOS - the cursor inside the field does not stand at the beginning, but stands where the finger was poked. Those. it turns out that a person dials not "+7 999 99 __", but "+7 ___ _1 23". Those. typing does not happen first.
I decided to force the cursor position to the very beginning, it worked on android, but on IOS everything remained the same. Below code:

var DOM_inputs = $('input[name=phone]');
$.fn.selectRange = function(start, end) {
                if(end === undefined) {
                    end = start;
                }
                return this.each(function() {
                    if('selectionStart' in this) {
                        this.selectionStart = start;
                        this.selectionEnd = end;
                    } else if(this.setSelectionRange) {
                        this.setSelectionRange(start, end);
                    } else if(this.createTextRange) {
                        var range = this.createTextRange();
                        range.collapse(true);
                        range.moveEnd('character', end);
                        range.moveStart('character', start);
                        range.select();
                    }
                });
            };
            DOM_inputs.inputmask({
                "mask": "+9 (999) 999 99 99",
                "autoclear": false
                // ,
                // "placeholder": ' ',
                // "oncomplete": function(){

                //  }
            });
            DOM_inputs.on('change click keyup', function(){
                var val = $(this).val();
                num = val.indexOf('_');
                console.log(num);
                if(num>=0)
                {
                    $(this).selectRange(num);
                }
                else{
                    $(this).selectRange(1);
                }
                
            });
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-11-14
@totalcount

I suggest not to suffer - jQuery Mask Plugin .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question