Go straight to code

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, Initial-scale =1.0"> <title>Document</title> </head> <body> <div class="container"> <input type="text" name="name" id="name" > <input > </div> <script> var input = document.getElementById('name') let typing = false input.addEventListener('compositionstart', function () { typing = true }) input.addEventListener('compositionend', function () { typing = false }) input.addEventListener('keyup', function () { if (! typing) { this.value = this.value.replace(/[^a-z0-9\u4E00-\u9FA5]/g,'') } }) </script> </body> </html>Copy the code

Compositionstart Indicates that the Chinese input method starts. Compositionend Trigger when the Chinese input method is finished. Keyup Triggered when the user releases a keyboard key.