Real Time Text Input or Textarea Filter Tutorial

Published :
Author :
Adam Khoury
In this JavaScript programming lesson you can learn Real-Time Filtering of Text Input. You can learn how to program real time strict text fields that will strip or replace unwanted characters from the field as the user types. Using the search, strip, or replace String object methods in JavaScript along with the RegExp object gives us a wide of logic possibilities in our JavaScript pattern matching with regular expressions. <script> function clean(el){ var textfield = document.getElementById(el); var regex = /[^a-z 0-9?!.,]/gi; if(textfield.value.search(regex) > -1) { textfield.value = textfield.value.replace(regex, ""); } } </script> <textarea id="ta" name="ta" onkeyup="clean('ta')" onkeydown="clean('ta')"></textarea>