jquery Verify that the phone number is correct example explain _jquery

Source: Internet
Author: User

If you want to do a cell phone number verification, then we need to know the number of mobile phone number section.
//Mobile number Attribution support Segment: 134 135 136 137 138 139 147 151 152 157 158 159 178 183 184 187 188

China Unicom number Attribution Support Number: 130 131 132 145 155 156 176 186

Telecom Number Attribution Support Number: 133 153 177 180 181 189
Mobile Operator: 170

Move:
2G Segment (GSM): 134-139, 150, 151, 152, 158-159;
Section 3G (TD-SCDMA): 157, 187, 188, 147.
Unicom:
2G Segment (GSM): 130-132, 155-156;
3G segment (WCDMA): 185, 186.
Telecom:
2G segment (CDMA): 133, 153;
Section 3G (CDMA2000): 180, 189.
You 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, introduce a jquery framework:

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

function to verify cell phone number:

 Verify the cell 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 =" cell 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) {//Hint error effect//jquery ("#phoneDiv"). Removeclass (). addclass ("Ui-form-item has-error");
         JQuery ("#phoneP"). HTML ("");
         JQuery ("#phoneP"). HTML ("<i class=\" Icon-error ui-margin-right10\ "> <\/i>" +message);
       JQuery ("#phone"). focus ();
         }else{//Prompts the correct effect//jquery ("#phoneDiv"). Removeclass (). addclass ("Ui-form-item has-success"); Jquery("#phoneP"). HTML ("");
       JQuery ("#phoneP"). HTML ("<i class=\" icon-success ui-margin-right10\ "> <\/i> the mobile number available");
     return flag;
 }

Send request to 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 checksum:

@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 query logic, find out if there is, then Mark 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);
    }
  

The above is the entire content of this article, teach you how to do jquery to verify the correct mobile phone number, using regular expression, we can try.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.