Romentigo's avatar

Regex: check a string

I have the next method:

isLetter(e) {
                let char = String.fromCharCode(e.keyCode);
                if(/^[А-я\' ІіЇїЄєҐґ]+$/.test(char)){
                    return true;
                }
                else e.preventDefault();
            },

This method doesn't allow to type any characters, except given above. It works on event @keypress. But I've noticed one problem – a user can just paste anything he wants in an input field. How can I validate the whole input field, for example, on @blur event?

0 likes
3 replies
MichalOravec's avatar

@romentigo You can use more event types for one handler like

$(document).on('change keyup input paste', '.your-element', function(e) {
    // your code
});
MaverickChan's avatar
Level 47

@romentigo of course you can apply multiple event on an element , like

<div @blur="yourfn()" @keyup="yourfn()" @keydown="yourfn()" @focus="yourfn()" @blur="yourfn()">
</div>
Romentigo's avatar

Thanks all for your replies! It helped a lot, both

Please or to participate in this conversation.