JQuery mask only alphabetic with strange behavior

0

I'm trying to add a mask to a field to accept only alphabetic characters.

Apparently I was able to find a solution .

Code

$(function () {

    $("#name").keypress( function(key) {

        if((key.charCode < 97 || key.charCode > 122) && (key.charCode < 65 || key.charCode > 90) && key.charCode != 32 ) {
            return false;
        }
    });
});

So far, it works cool, it only accepts letters ( [a-zA-Z] ) and espaço only.

But the bizarre that after entering any sequence of letters, after giving espaço twice appears a ponto of nothing.

If I try to explicitly prohibit the permission of ponto including key.charCode == 46 in if buga everything and accepts any type of character.

Does anyone have any idea how to make it work correctly?

    
asked by anonymous 21.11.2018 / 20:47

1 answer

0

But what are you doing with this eventListener? You're only returning false if the character is not what you want, what does it mean?

There is nothing to analyze in the code, the code posted is not even a mask. If you want to make a mask for letters and spaces, try something like:

$(function () {

    $("#name").on('input', function(e) {
        this.value = this.value.replace(/[^a-zà-ÿ\s]/ig, '');
    });

});
    
21.11.2018 / 21:24