JS

Phone no format script

Add the following script to change input type text to phone no format.


jQuery('#phone-field').on('keyup', function(e){
let val = e.target.value;
e.target.value = val
.replace(/\D/g, '')
.replace(/(\d{1,3})(\d{1,3})?(\d{1,4})?/g, function(txt, f, s, t) {
if (t) {
return `(${f}) ${s}-${t}`
} else if (s) {
return `(${f}) ${s}`
} else if (f) {
return `(${f})`
}
});
});

Advertisement

Did you find this helpful? Share it!

Leave a Comment

Your email address will not be published. Required fields are marked