js結(jié)合正則實(shí)現(xiàn)國內(nèi)手機(jī)號段校驗(yàn)
附加一個(gè)utils對象,內(nèi)含一個(gè)校驗(yàn)手機(jī)號函數(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}$/; //移動方面最新答復(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號段 電信方面沒給出答復(fù),視作不存在
var isOtherTelphone = /^170([059])\d{7}$/;//其他運(yùn)營商
var utils = {
checkMobile: function(telphone){
telphone = this.trim(telphone);
if(telphone.length !== 11){
return this.setReturnJson(false, '未檢測到正確的手機(jī)號碼');
}
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, '未檢測到正確的手機(jī)號碼');
}
}
},
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號碼段的手機(jī)號碼
<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('請輸入有效的手機(jī)號碼!');
return false;
}
</script>
以上代碼是在jquery下調(diào)試的。
不需要jquery的代碼
function validatemobile(mobile)
{
if(mobile.length==0)
{
alert('請輸入手機(jī)號碼!');
document.form1.mobile.focus();
return false;
}
if(mobile.length!=11)
{
alert('請輸入有效的手機(jī)號碼!');
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('請輸入有效的手機(jī)號碼!');
document.form1.mobile.focus();
return false;
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法分析
這篇文章主要介紹了JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法,結(jié)合實(shí)例形式分析了javascript字符串使用reverse方法、字符串遍歷方法以及針對輸入字符串的遍歷、逆序輸出等方法實(shí)現(xiàn)字符串反轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
BootstrapTable加載按鈕功能實(shí)例代碼詳解
這篇文章主要介紹了BootstrapTable加載按鈕功能實(shí)例代碼詳解,需要的朋友可以參考下2017-09-09
echarts實(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-02
JavaScript?對象新增方法defineProperty與keys的使用說明
這篇文章主要介紹了JavaScript對象新增方法defineProperty與keys的使用說明,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
如何通過IntersectionObserver實(shí)現(xiàn)懶加載
這篇文章主要介紹了通過IntersectionObserver實(shí)現(xiàn)懶加載,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

