2016-07-25 81 views
-1

我有暫定的新電話號碼 - 559907XXXX。我需要將其格式更改爲(111)-111-1111。這個電話號碼是在跨度和輸入字段ot,所以我無法使用掩碼。使用javascript的DOM中的電話號碼格式

$('.phone-number').mask('(000)-000-0000'); 

尋找解決方案使用REG前。請幫助

+0

告訴我們你到目前爲止試過了什麼? – spirit

回答

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> 
+0

請嘗試此操作,無論您是否期望相同 – Ray

+0

只有這在事件處理程序中足夠了this.value = this.value.replace(/ \ D/g,'').replace(/(\ d {3}) (\ d {4})/,'($ 1)$ 2- $ 3');' – Tushar

+0

在我的情況中,最後4位數字不是數字,而是XXXX,格式應該是(559)-907-XXXX 。所以我沒有得到正確的迴應使用上面提出的兩種方式。 – user2947