Create an input element on the page with a placeholder” enter your name” and an H2 heading on the page inside HTML. The purpose of this input element is to enter a user’s name so it should only input. letters from a-z, A-Z and space (all other characters should not be detected). Whenever the user inputs their name, their input should be dynamically visible inside the heading. [Please note that no other character apart from the allowed characters should be visible in the heading]
HTML CODE Inside body
<h2></h2>
<input placeholder="Enter your name" />
JavaScript Code
let input = document.querySelector("input");
let h2 = document.querySelector("h2");
input.addEventListener("input",function(){
console.log(input.value);
h2.innerText=input.value;
});
Use
beforeinputevent and test whetherEvent::datamatches your allowed characters. If not, callEvent::preventDefault()to cancel the changing of the input: