jQuery-Mask-Plugin icon indicating copy to clipboard operation
jQuery-Mask-Plugin copied to clipboard

Cursor Positioning

Open Yelldon opened this issue 9 years ago • 3 comments

I have an issue and I was going to see if anyone else was having the same issue. The masking seems to cause an issue making the cursor jump to another position that what was intended. I have a formatted phone number field that is masked, like this:

(123) 456-7890

The masking works fine, but, say, if you clear the field, not by a back space, but by typing in another number, you get the first two characters flipped, this this:

(213) 456-7890

That is if you clear the field by typing a 1 and then a 2. The issue seems to be that it is repositioning the cursor behind what you just typed and then when typing again, positions at the end. This also does this if you remove anything up to a masked element. For example the ')', if you select all values up to that and type, it will flip the first two characters.

I've looked through the issues, and I think issue #403 may possibly be the issue that caused this, but I can't verify that yet. Right now I have a work around when I unmask the first two characters when clearing the field (or starting it), but this doesn't effect characters running up against the mask elements added. Here is what I'm doing:

            var defaultMaskOptions = {
                onKeyPress: function(cep, e, field) {
                    var $field = $(field),
                        $fieldVal = $field.cleanVal();
                    if ($fieldVal.length < 2) {
                        $field.val('').val($fieldVal);
                    }
                }
            };

If anyone else has a better work around, I would love to hear it. When I get the chance, I'll try and figure out exactly why it's happening and make a PR.

Yelldon avatar Apr 10 '17 14:04 Yelldon

I'm also seeing this behavior in Chrome: Version 57.0.2987.133 (64-bit) (not tested in other browsers). I noticed after I upgraded from 1.13.9 to 1.14.10.

http://g.recordit.co/XYMGBDGKYs.gif

gmhenderson avatar Apr 21 '17 16:04 gmhenderson

Having the same problem.

jakelly avatar Apr 28 '17 21:04 jakelly

I believe this is fixed now.

igorescobar avatar May 30 '17 21:05 igorescobar