/ Published in: jQuery
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.
Eg, As the number "0000000000" was typed, it would convert it to (000)-000-0000.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script type="text/javascript"> $(function() { $("input[@name='phone']").keyup(function() { var curchr = this.value.length; var curval = $(this).val(); if (curchr == 3) { $("input[@name='phone']").val("(" + curval + ")" + "-"); } else if (curchr == 9) { $("input[@name='phone']").val(curval + "-"); } }); }); </script>