There is an amazing jquery plugin for this kind of inputs:
http://digitalbush.com/projects/masked-input-plugin/
the code:
First include javascript jQuery files and masked input files.
<script src="jquery.js" type="text/javascript"></script> <script src="jquery.maskedinput.js" type="text/javascript"></script>
Then call the mask function for those elements that you want to mask.
jQuery(function($){ $("#date").mask("99/99/9999"); $("#phone").mask("(999) 999-9999"); $("#tin").mask("99-9999999"); $("#ssn").mask("999-99-9999"); });
Optionally, if you are not satisfied with the underscore ('_') as a placeholder, you can pass an optional argument to maskedinput.
jQuery(function($){ $("#product").mask("99/99/9999",{placeholder:" "}); });
Optionally, if you want to execute the function after the mask completes, you can specify this function as an optional argument to the maskedinput method.
jQuery(function($){ $("#product").mask("99/99/9999",{completed:function(){alert("You typed the following: "+this.val());}}); });
etc.
source share