js結(jié)合正則實(shí)現(xiàn)國(guó)內(nèi)手機(jī)號(hào)段校驗(yàn)
附加一個(gè)utils對(duì)象,內(nèi)含一個(gè)校驗(yàn)手機(jī)號(hào)函數(shù),一個(gè)格式化返回?cái)?shù)據(jù)函數(shù)
var isChinaMobile = /^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/; //移動(dòng)方面最新答復(fù) var isChinaUnion = /^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/; //向聯(lián)通微博確認(rèn)并未回復(fù) var isChinaTelcom = /^(?:133|153|177|18[019])\d{8}$/; //1349號(hào)段 電信方面沒(méi)給出答復(fù),視作不存在 var isOtherTelphone = /^170([059])\d{7}$/;//其他運(yùn)營(yíng)商 var utils = { checkMobile: function(telphone){ telphone = this.trim(telphone); if(telphone.length !== 11){ return this.setReturnJson(false, '未檢測(cè)到正確的手機(jī)號(hào)碼'); } else{ if(isChinaMobile.test(telphone)){ return this.setReturnJson(true, '移動(dòng)', {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, '未檢測(cè)到正確的手機(jī)號(hào)碼'); } } }, 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 }; } }
驗(yàn)證130-139,150-159,180-189號(hào)碼段的手機(jī)號(hào)碼
<script type="text/javascript"> var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; if(!myreg.test($("#phone").val())) { alert('請(qǐng)輸入有效的手機(jī)號(hào)碼!'); return false; } </script>
以上代碼是在jquery下調(diào)試的。
不需要jquery的代碼
function validatemobile(mobile) { if(mobile.length==0) { alert('請(qǐng)輸入手機(jī)號(hào)碼!'); document.form1.mobile.focus(); return false; } if(mobile.length!=11) { alert('請(qǐng)輸入有效的手機(jī)號(hào)碼!'); document.form1.mobile.focus(); return false; } var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; if(!myreg.test(mobile)) { alert('請(qǐng)輸入有效的手機(jī)號(hào)碼!'); document.form1.mobile.focus(); return false; } }
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法分析
這篇文章主要介紹了JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法,結(jié)合實(shí)例形式分析了javascript字符串使用reverse方法、字符串遍歷方法以及針對(duì)輸入字符串的遍歷、逆序輸出等方法實(shí)現(xiàn)字符串反轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下2018-08-08BootstrapTable加載按鈕功能實(shí)例代碼詳解
這篇文章主要介紹了BootstrapTable加載按鈕功能實(shí)例代碼詳解,需要的朋友可以參考下2017-09-09echarts實(shí)現(xiàn)3d柱狀圖的2種方式舉例
echarts3D效果柱狀圖的實(shí)現(xiàn),這個(gè)太難了,我花了兩天終于調(diào)成我想要的效果啦,要是官網(wǎng)上有例子就好了,太難調(diào)了,下面這篇文章主要給大家介紹了關(guān)于echarts實(shí)現(xiàn)3d柱狀圖的2種方式,需要的朋友可以參考下2023-02-02js實(shí)現(xiàn)可拖動(dòng)DIV的方法
這篇文章主要介紹了js實(shí)現(xiàn)可拖動(dòng)DIV的方法,有需要的朋友可以參考一下2013-12-12JavaScript?對(duì)象新增方法defineProperty與keys的使用說(shuō)明
這篇文章主要介紹了JavaScript對(duì)象新增方法defineProperty與keys的使用說(shuō)明,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09如何通過(guò)IntersectionObserver實(shí)現(xiàn)懶加載
這篇文章主要介紹了通過(guò)IntersectionObserver實(shí)現(xiàn)懶加載,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04