附加一個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號碼段的手機號碼
<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('請輸入有效的手機號碼!');
return false;
}
</script>
以上代碼是在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;
}
}
以上所述就是本文的全部內容了,希望大家能夠喜歡。