jquery verifies that the phone number is correct

  • 2020-11-18 05:15:52
  • OfStack

If we want to do the phone number verification, then we need to know the phone number segment.
// The mobile number belongs to the area supported: Paragraph 134 135 136 137 138 139 147 150 151 152 157 158 159 178 182 182 183 184 187 188

// Support section of Unicom number :130 131 132 145 155 156 176 186

// Where the telecommunications number belongs, support paragraph :133 153 177 180 181 189
// Mobile operators :170

Mobile:
Section 2G (GSM) : 134-139, 150, 151, 152, 158-159;
Paragraph 3G (TD-ES15en) : 157, 187, 188, 147.
Unicom:
2G (GSM) : 130-132, 155-156;
Paragraph 3G (WCDMA) : 185, 186.
Telecommunication:
Section 2G (CDMA) : 133, 153;
Section 3G (CDMA2000) : 180, 189.
Can write a regular expression: var myreg = / ^ (((13 [0-9] {1}) | (14 [0-9] {1}) | (17 [0] {1}) | (15 [0, 3] {1}) | (15 [5-9] {1}) | (18 [0-9] {1})) + \ d {8}) $/;
< input type="text" id="phone" name="phone" / >
First, an JQuery framework is introduced:


<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
</script>

Function of checking mobile phone number:


 // Verify the phone number 
     function vailPhone(){
       var phone = jQuery("#phone").val();
       var flag = false;
       var message = "";
       var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;       
       if(phone == ''){
         message = " The mobile phone number cannot be empty! ";
       }else if(phone.length !=11){
         message = " Please enter a valid mobile phone number! ";
       }else if(!myreg.test(phone)){
         message = " Please enter a valid mobile phone number! ";
       }else if(checkPhoneIsExist()){
         message = " The phone number has been bound! ";
       }else{
           flag = true;
       }
       if(!flag){
      // Prompt error effect 
         //jQuery("#phoneDiv").removeClass().addClass("ui-form-item has-error");
         //jQuery("#phoneP").html("");
         //jQuery("#phoneP").html("<i class=\"icon-error ui-margin-right10\">&nbsp;<\/i>"+message);
         //jQuery("#phone").focus();
       }else{
            // Prompt correct effect 
         //jQuery("#phoneDiv").removeClass().addClass("ui-form-item has-success");
         //jQuery("#phoneP").html("");
         //jQuery("#phoneP").html("<i class=\"icon-success ui-margin-right10\">&nbsp;<\/i> The cell phone number is available ");
       }
       return flag;
     }

Send the request to the background:


// Verify that the phone number exists 
       function checkPhoneIsExist(){
         var phone = jQuery("#phone").val();
         var flag = true;
         jQuery.ajax(
          { url: "checkPhone?t=" + (new Date()).getTime(),
            data:{phone:phone},
            dataType:"json",
               type:"GET",
               async:false,
               success:function(data) {
               var status = data.status;
               if(status == "0"){
                 flag = false;
               }
             }
        });
        return flag;
       }

java back-end check:


@RequestMapping(value = "/checkPhone", method = RequestMethod.GET)
  public void checkPhone(HttpServletRequest request,HttpServletResponse response) {
    
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      String phone = request.getParameter("phone");
      String status = "0";
      // Write the query logic and, if found, mark as 1 , otherwise marked as 0
            //UserCellphoneAuth userCellphoneAuth = userService.findUserCellphoneAuthByPhone(phone);
      //if(userCellphoneAuth!=null){
      //  status = "1";
      //}
      map.put("status", status);
      String data = JSONObject.fromObject(map).toString();      
      response.getWriter().print(data);
      response.getWriter().flush();
      response.getWriter().close();

    } catch (Exception ex) {
      logger.error(ex.getMessage(), ex);
    }
  }

That's the end of this article, how to use jquery to verify that the phone number is correct, using regular expressions, you can give it a try.


Related articles: