JavaScript判斷手機號運營商是移動、聯(lián)通、電信還是其他(代碼簡單)
更新時間:2015年09月25日 11:32:07 投稿:mrr
本文由腳本之家小編給大家分享的基于js判斷手機號運行商是移動、聯(lián)通、電信還是其他的,然后根據(jù)不同的運營商做出對應(yīng)的處理,感興趣的朋友一起學(xué)習(xí)吧
正則表達式判斷所填入號碼的運營商js代碼修改版:http://m.fzitv.net/article/31563.htm
在做WEB項目時,有時候需要根據(jù)用戶的輸入手機號碼判斷該號的運營商是移動、聯(lián)通、電信或其他,再根據(jù)不同的運營商做出相應(yīng)的處理,下面介紹js中如何判斷手機號的運營商的代碼
純js代碼
var isChinaMobile = /^134[0-8]\\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\\d{8}$/; //移動方面最新答復(fù)
var isChinaUnion = /^(?:13[0-2]|145|15[56]|176|18[56])\\d{8}$/; //向聯(lián)通微博確認并未回復(fù)
var isChinaTelcom = /^(?:133|153|177|18[019])\\d{8}$/; //1349號段 電信方面沒給出答復(fù),視作不存在
var isOtherTelphone = /^170([059])\\d{7}$/;//其他運營商
var utils = {
checkMobile: function(telphone){
telphone = this.trim(telphone);
if(telphone.length !== 11){
return this.setReturnJson(false, '未檢測到正確的手機號碼');
}
else{
if(isChinaMobile.test(telphone)){
return this.setReturnJson(true, '移動', {name: 'ChinaMobile'});
}
else if(isChinaUnion.test(telphone)){
return this.setReturnJson(true, '聯(lián)通', {name: 'ChinaUnion'});
}
else if(isChinaTelcom.test(telphone)){
return this.setReturnJson(true, '電信', {name: 'ChinaTelcom'});
}
else if(isOtherTelphone.test(telphone)){
var num = isOtherTelphone.exec(telphone);
return this.setReturnJson(true, '', {name: ''});
}
else{
return this.setReturnJson(false, '未檢測到正確的手機號碼');
}
}
},
setReturnJson: function(status, msg, data){
if(typeof status !== 'boolean' && typeof status !== 'number'){
status = false;
}
if(typeof msg !== 'string'){
msg = '';
}
return {
'status': status,
'msg': msg,
'data': data
};
}
}
怎么樣,以上代碼超簡單吧,希望對大家學(xué)習(xí)js判斷手機號運行尚有所幫助。
相關(guān)文章
JS中數(shù)組實現(xiàn)代碼(倒序遍歷數(shù)組,數(shù)組連接字符串)
這篇文章主要介紹了JS中數(shù)組實現(xiàn)代碼(倒序遍歷數(shù)組,數(shù)組連接字符串),代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
ECharts?formatter屬性設(shè)置的3種方法(字符串模板,函數(shù)模板,回調(diào)函數(shù))
formatter有兩種寫法,一種字符串模板,另一種是回調(diào)函數(shù),下面這篇文章主要給大家介紹了關(guān)于ECharts?formatter屬性設(shè)置的3種方法,分別是字符串模板,函數(shù)模板,回調(diào)函數(shù),需要的朋友可以參考下2023-02-02

