Using RegExp to allow only one space in between words - Javascript RegExp

Javascript examples for RegExp:RegExp test

Description

Using RegExp to allow only one space in between words

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){//from  www .  j a  v a  2s  .c o  m
$('.name').keyup(function() {
    var $th = $(this);
    $th.val($th.val().replace(/(\s{2,})|[^a-zA-Z']/g, ' '));
    $th.val($th.val().replace(/^\s*/, ''));
    });
    });

      </script> 
 </head> 
 <body> 
  <input type="test" class="name">  
 </body>
</html>

Related Tutorials