Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Created October 26, 2016 07:36
Show Gist options
  • Save greathmaster/296a3053c56eaec7c762093633aecfdc to your computer and use it in GitHub Desktop.
Save greathmaster/296a3053c56eaec7c762093633aecfdc to your computer and use it in GitHub Desktop.
Actively formats phone number into XXX-XXX-XXXX format
//Actively formats phone number into XXX-XXX-XXXX format
function format_phone_number()
{
//Adapted from: http://stackoverflow.com/questions/26412623/jquery-or-javascript-to-autoformat-phone-mask
echo
"<script>
<!--
jQuery('#bphone').keyup(function(ev) {
var key = ev.which;
if (key < 48 || key > 57 || key != 45) {
ev.preventDefault();
}
if (this.value.length > 12) {
this.value = this.value.slice(0, -1);
return;
}
this.value = this.value.replace(/^(\d{3})(\d)/, '$1-$2').replace(/^(\d{3}-\d{3})(\d)/, '$1-$2');
});
-->
</script>";
}
add_action("pmpro_checkout_after_form", "format_phone_number");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment