js結(jié)合正則實現(xiàn)國內(nèi)手機(jī)號段校驗
更新時間:2015年06月19日 09:32:49 投稿:hebedich
這篇文章主要介紹了js結(jié)合正則實現(xiàn)國內(nèi)手機(jī)號段校驗的方法以及使用js和jQuery實現(xiàn)的簡單校驗手機(jī)號的示例,非常簡單實用,有需要的小伙伴可以參考下。
附加一個utils對象,內(nèi)含一個校驗手機(jī)號函數(shù),一個格式化返回數(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}$/;//其他運營商
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
};
}
}
驗證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)文章
JavaScript?對象新增方法defineProperty與keys的使用說明
這篇文章主要介紹了JavaScript對象新增方法defineProperty與keys的使用說明,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-09-09
如何通過IntersectionObserver實現(xiàn)懶加載
這篇文章主要介紹了通過IntersectionObserver實現(xiàn)懶加載,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04

