javascript phone number mask

Solutions on MaxInterview for javascript phone number mask by the best coders in the world

showing results for - "javascript phone number mask"
Alice
10 Feb 2020
1// JAVASCRIPT
2document.getElementById('phone').addEventListener('input', function (e) {
3  var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
4  e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : '');
5});
6
7// HTML
8<input type="text" id="phone" placeholder="(555) 555-5555"/>