jQuery snippet to convert numbers into US phone number format as they're typed


/ Published in: jQuery
Save to your folder(s)

This little piece of code will convert to US phone format, as typed.
Eg, As the number "0000000000" was typed, it would convert it to (000)-000-0000.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(function() {
  3. $("input[@name='phone']").keyup(function() {
  4. var curchr = this.value.length;
  5. var curval = $(this).val();
  6. if (curchr == 3) {
  7. $("input[@name='phone']").val("(" + curval + ")" + "-");
  8. } else if (curchr == 9) {
  9. $("input[@name='phone']").val(curval + "-");
  10. }
  11. });
  12. });
  13. </script>

Report this snippet


Comments


You need to login to view comments.