'Javascript, convert inline html js to normal js

So I have this input tag:

<input ... onkeypress="return /^[A-Za-z0-9 -]*$/.test(event.key)" id="id_title">

And I want it to accept only numbers, letters and spaces. I wonder how to rewrite it in my JS file with jQuery in modern JS version, because all methods from the Internet with old JS are crossed out. Like in here, https://www.codegrepper.com/code-examples/javascript/allow+only+letters+in+input+javascript onkeypress or keyCode methods are not working now. They are crossed out.

$('#id_title').on('keydown', function() {  

     let regex = /^[A-Za-z0-9 -]*$/ // accepts only letters, numbers and spaces
     ... // how to continue?

}

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source