[Vuejs]-Vue.js: How to prevent the user from entering characters other than letters and numbers

0👍

You can put something like this as the onKeyPress event handler:

(event) => {
    if (!event.key.match(/[0-9a-zA-Z]/g)) {
        event.preventDefault();
    }
}

This will prevent key-presses outside the alphanumeric range from being entered. However, note that this is not foolproof (e.g. it will still allow you to copy-paste symbols into the field), so you might still want some sort of validation.

Leave a comment