這篇文章主要介紹了js結(jié)合正則實(shí)現(xiàn)國(guó)內(nèi)手機(jī)號(hào)段校驗(yàn)的方法以及使用js和jQuery實(shí)現(xiàn)的簡(jiǎn)單校驗(yàn)手機(jī)號(hào)的示例,非常簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
附加一個(gè)utils對(duì)象,內(nèi)含一個(gè)校驗(yàn)手機(jī)號(hào)函數(shù),一個(gè)格式化返回?cái)?shù)據(jù)函數(shù)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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)碼
?
1 2 3 4 5 6 7 8 <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 f新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注