js combined with regular realization of mobile phone segment calibration in China

  • 2020-06-15 07:47:36
  • OfStack

Attach 1 utils object, including 1 check phone number function and 1 format return data function


var isChinaMobile = /^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/; // Latest answers on mobile 
var isChinaUnion = /^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/; // There was no reply to China Unicom's weibo 
var isChinaTelcom = /^(?:133|153|177|18[019])\d{8}$/; //1349 Them roughly   Telecom did not reply and deemed it nonexistent 
var isOtherTelphone  = /^170([059])\d{7}$/;// Other operators 
 
var utils = {
  checkMobile: function(telphone){
    telphone = this.trim(telphone);
    if(telphone.length !== 11){
      return this.setReturnJson(false, ' The correct cell phone number was not detected ');
    }
    else{
      if(isChinaMobile.test(telphone)){
        return this.setReturnJson(true, ' mobile ', {name: 'ChinaMobile'});
      }
      else if(isChinaUnion.test(telphone)){
        return this.setReturnJson(true, ' unicom ', {name: 'ChinaUnion'});
      }
      else if(isChinaTelcom.test(telphone)){
        return this.setReturnJson(true, ' telecom ', {name: 'ChinaTelcom'});
      }
      else if(isOtherTelphone.test(telphone)){
        var num = isOtherTelphone.exec(telphone);
        return this.setReturnJson(true, '', {name: ''});
      }
      else{
        return this.setReturnJson(false, ' The correct cell phone number was not detected ');
      }
    }
  },
  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
    };
  }
}

Verify the phone number in segment 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(' Please enter a valid mobile phone number! '); 
  return false; 
} 
</script>

The above code was debugged under jquery.

No jquery code is required


function validatemobile(mobile) 
  { 
    if(mobile.length==0) 
    { 
     alert(' Please enter your mobile phone number! '); 
     document.form1.mobile.focus(); 
     return false; 
    }   
    if(mobile.length!=11) 
    { 
      alert(' Please enter a valid mobile phone number! '); 
      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(' Please enter a valid mobile phone number! '); 
      document.form1.mobile.focus(); 
      return false; 
    } 
  } 

This is the end of this article, I hope you enjoy it.


Related articles: