我有一个暂定的新电话号码--559907XXXX。我需要将其格式更改为(111)-111-1111。这个电话号码在span范围内,在输入字段中没有,所以我无法使用掩码。
$('.phone-number').mask('(000)-000-0000');
使用reg ex寻找解决方案。请帮忙
答案 0 :(得分:0)
<html>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("#phone").blur( function (e) {
var x = e.target.value.replace(/\D/g, '').match(/(\d{3})(\d{3})(\d{4})/);
e.target.value = '(' + x[1] + ') ' + x[2] + '-' + x[3];
});
});
</script>
<body>
<p>Masked on the blur event (remove focus).</p>
<input type="text" id="phone" placeholder="(555) 555-5555"/>
</body>
</html>