javascript - Auto Format Phone Number in Jquery -
i have 1 textbox phone number. phone number should xxx-xxx-xxx format.
i got solution xxx-xxx-xxx format, don't know how modify code.
$('#ssn').keyup(function() { var val = this.value.replace(/\d/g, ''); var newval = ''; while (val.length > 3) { newval += val.substr(0, 3) + '-'; val = val.substr(3); } newval += val; this.value = newval; });
since you're using jquery can try jquery masked-input-plugin.
there's jsfiddle here can see how works.
the source code project on github can found here.
the implementation more simple:
html:
<input id="ssn"/>
javascript:
$("#ssn").mask("999-999-999");
update:
another 1 can found here.
Comments
Post a Comment