javascript - Append +1 to phone number field value -
i need format phone number field correctly , have done.
i able use script add dashes in phone number how can add "+1" front of phone number when 10 numbers have been typed?
$('#tel').keyup(function(){ $(this).val($(this).val().replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'$1-$2-$3')) });
i first suggest replace keyup
event input
(which has ie 10+ support) if @ possible. next, should rework filter true numeric value first. work regex against that. in example replaced non-10-digit-numbers dash separated values, trimmed resulting trailing dashes, , replaced full 10-digit-value same value , +1
@ beginning.
$('#tel').on('input', function(){ var filteredvalue = this.value.replace('+1 ', '').match(/\d*/g).join(''); $(this).val(filteredvalue .replace(/(\d{0,3})\-?(\d{0,3})\-?(\d{0,4}).*/,'$1-$2-$3') .replace(/\-+$/, '') .replace(/(\d{3})\-?(\d{3})\-?(\d{4})/,'+1 $1-$2-$3')) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id=tel>
here's simplified version closer original makes replacement @ end of having 10 digits.
$('#tel').on('input', function(){ var filteredvalue = this.value.replace('+1 ', '').match(/\d*/g).join(''); $(this).val(filteredvalue .replace(/(\d{3})\-?(\d{3})\-?(\d{4}).*/,'+1 $1-$2-$3')) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id=tel>
Comments
Post a Comment