How To Make An Input Text Accept Only Alphabet Values Using Javascript
In this Javascript tutorial, we will see how to create an input text field that only allows letter characters to be entered during the 'keypress' event.
We will accomplish this by using the 'test' function in JS and Netbeans editor .
Project Source Code:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="text" onkeydown="isInputLetter(event)">
<script>
function isInputLetter(evt){
var ch = String.fromCharCode(evt.which);
if(!(/[a-zA-Z]/.test(ch) || ch === " " || evt.keyCode === 8)){
evt.preventDefault();
}
console.log(evt.keyCode);
}
</script>
</body>
</html>
OUTPUT: