jquery驗證手機號是否正確執行個體講解,jquery執行個體講解

來源:互聯網
上載者:User

jquery驗證手機號是否正確執行個體講解,jquery執行個體講解

如果要做手機號的驗證,那麼我們需要知道手機號碼的號段。
//移動號碼歸屬地支援號段:134 135 136 137 138 139 147 150 151 152 157 158 159 178  182 183 184 187 188

//聯通號碼歸屬地支援號段:130 131 132  145 155 156 176  186 

//電訊號碼歸屬地支援號段:133 153 177 180 181 189 
//行動電信業者:170

移動:
2G號段(GSM):134-139、150、151、152、158-159;
3G號段(TD-SCDMA):157、187、188、147.
聯通:
2G號段(GSM):130-132、155-156;
3G號段(WCDMA):185、186.
電信:
2G號段(CDMA):133、153;
3G號段(CDMA2000):180、189.
可以寫出一個Regex: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" />
首先引入一個JQuery架構:

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

 校正手機號的函數:

 //驗證手機號     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 = "手機號碼不可為空!";       }else if(phone.length !=11){         message = "請輸入有效手機號碼!";       }else if(!myreg.test(phone)){         message = "請輸入有效手機號碼!";       }else if(checkPhoneIsExist()){         message = "該手機號碼已經被綁定!";       }else{           flag = true;       }       if(!flag){      //提示錯誤效果         //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{            //提示正確效果         //jQuery("#phoneDiv").removeClass().addClass("ui-form-item has-success");         //jQuery("#phoneP").html("");         //jQuery("#phoneP").html("<i class=\"icon-success ui-margin-right10\"> <\/i>該手機號碼可用");       }       return flag;     }

發送請求給後台:

//驗證手機號是否存在       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後端進行校正:

@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";      //寫查詢邏輯,查出有的話,那麼標記為1,否則標記為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);    }  }

以上就是本文的全部內容,教大家如何進行jquery驗證手機號是否正確,利用Regex,大家可以動手試一試。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.