Use regex to filter text - Javascript String Operation

Javascript examples for String Operation:String Replace

Description

Use regex to filter text

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <textarea id="ta" name="ta" onkeyup="clean('ta')" onkeydown="clean('ta')"></textarea> 
      <script type="text/javascript">
    function clean(e) {/* w  ww . ja  v a  2s . com*/
        var textfield = document.getElementById(e);
        var regex = /[a-zA-Z]/gi;
        textfield.value = textfield.value.replace(regex, "");
    }

      </script>  
   </body>
</html>

Related Tutorials