您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

js结合正则实现国内手机号段校验_javascript技巧

2025/2/18 4:59:41发布24次查看
附加一个utils对象,内含一个校验手机号函数,一个格式化返回数据函数
var ischinamobile = /^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/; //移动方面最新答复var ischinaunion = /^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/; //向联通微博确认并未回复var ischinatelcom = /^(?:133|153|177|18[019])\d{8}$/; //1349号段 电信方面没给出答复,视作不存在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, '联通', {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 }; }}
验证130-139,150-159,180-189号码段的手机号码

以上代码是在jquery下调试的。
不需要jquery的代码
function validatemobile(mobile) { if(mobile.length==0) { alert('请输入手机号码!'); document.form1.mobile.focus(); return false; } if(mobile.length!=11) { alert('请输入有效的手机号码!'); 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('请输入有效的手机号码!'); document.form1.mobile.focus(); return false; } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product