<script type="text/javascript"> window.onload =function() { document.getElementById("bank_account_id").onkeyup =function() { this.value =this.value.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 "); }; }; </script>