Restrict Text Input Characters HTML JavaScript Tutorial

Published :
Author :
Adam Khoury

Learn to restrict one or more text fields in a form to allow only characters that you desire to be typed into them. We will remove undesirable characters in real time as the user types.

<script> function lettersOnly(input) { var regex = /[^a-z]/gi; input.value = input.value.replace(regex, ""); } </script> <input id="firstname" placeholder="first name" onkeyup="lettersOnly(this)"> <input id="lastname" placeholder="last name" onkeyup="lettersOnly(this)">