Format Ph Input
by Christopher O
HTML
<input name="ph" type="text" id="ph" class="txtInput" value="" placeholder="Phone Number" />
JavaScript
// Format phone number input (Chris 2015)
$(document).on('change', '#ph', function(){
var ph = $(this).val();
ph = ph.replace(/[^0-9]+/g, '');// strip non-digits
if( ph.length == 10 && /^(?!(.)\D*(.)(\D*\1\D*\2){4})/.test(ph)){
ph = ph.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"); // format
}else{
ph = '';
$(this).attr("placeholder", "A Real 10-digit Phone No.");
}
$(this).val(ph);
});