javascript - jquery Telephone Format -
i trying output user inputted value of 1234567890 displayed 123-456-7890. below code, output expected when trying edit number, there weird issues. can please help.
$("input[type='tel']").keyup(function() { if (this.value.length === 10) { var number = this.value this.value = number.substring(0, 3) + '-' + number.substring(3, 6) + '-' + number.substring(6, 10) } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input class="form-control" placeholder="e.g.: 111-222-3333" type="tel" maxlength="10" />
the problem not removing "-" previous value. when keyup event fired, value has - previous keyup parse event.
why don´t remove "-" string, , run substring code?
it'd like
$("input[type='tel']").keyup(function() { if (this.value.length === 10) { var number = this.value.replace('-', ''); this.value = number.substring(0, 3) + '-' + number.substring(3, 6) + '-' + number.substring(6, 10) } });
Comments
Post a Comment