Error in Maskinput Javascript

Asked

Viewed 127 times

3

I’m having problems in a form field that is masked through the script below.

MaskInput(document.getElementById('xxx'),'9999');

According to the function it should format the 4 characters of the field in numerical format and accepting any number of 0-9.

The problem is that using IE 9+ is only accepting character 8, that is, it does not accept any other number.

Any idea what might be going on?

        MaskInput = function(f, m){
       function mask(e){
           var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[\xC0-\xFF]/i, "8": /./ },
               rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
           function accept(c, rule){
               for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                   if(r & i && patterns[i].test(c))
                       break;
                   return i <= r || c == rule;
           }
           var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
           (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
               r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
               : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
               r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
       }
       for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
           addEvent(f, i, mask);
    };

    addEvent = function(o, e, f, s){
        var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
        r[r.length] = [f, s || o], o[e] = function(e){
            try{
                (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
                e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
                e.target || (e.target = e.srcElement || null);
                e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
            }catch(f){}
            for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
            return e = null, !!d;
        }
    };

    removeEvent = function(o, e, f, s){
        for(var i = (e = o["_on" + e] || []).length; i;)
            if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
                return delete e[i];
        return false;
    };
  • 1

    Works well for me... -> https://jsfiddle.net/2y5z1zd8/, you can see my example and explain what doesn’t work?

  • @Sergio Seu fiddle presents the same problem to me using Firefox. In Chrome it worked normal. I think it’s compatibility problem.

  • Okay, so the problem is with the String.fromCharCode that Firefox is tricky. You have to use this plugin? or you can explain what you want to do and we’ll help you make your own?

  • @Sergio tested his example in firefox and IE11, and presents the same problem described in the question, accepts only character 8, but responding to his question, it is not necessary to use the same plugin, but changing the plugin means fixing 258 source files that use the function. If you could understand/correct the problem with this mask, it would be ideal, some idea?

  • @Tannerse but what is the function of the plugin? prevent the input from being different from number? that is when the client clicks a letter he simply ignores, and accepts only numbers, at most four numbers?

  • @Sergio exactly, must prevent the user to type letters and must accept only numbers.

  • @Tannerse would then work something like this: https://jsfiddle.net/scruv9s4/ ?

  • @Sergio works partially, because when typing the numbers he goes erasing what was typed, he accepts only hold tight some number.

  • @Tannerif you’re right, I’m working on other things and I sent you a wrong version. However, I’ve reworked it, simpler: https://jsfiddle.net/j9bkg0nt/

  • @Sergio, this version of you works very well, but I think I found a fix for the first version: https://jsfiddle.net/om86y7ew/ replaces String.fromCharCode(k = e.key) with String.fromCharCode(k = e.keycode)

  • Dude, I suggest you use another plugin that works very well and is very easy to install

Show 6 more comments

1 answer

0

In the Firefox, it is possible to use e.which for the same purpose. This property is not standard.

A more reliable approach would be to use the solution below to recover the Keycode:

           var k, mC, r, c = String.fromCharCode(k = (e.which || e.keyCode)), l = f.value.length;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.