欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery驗證手機(jī)號是否正確實例講解

 更新時間:2015年11月17日 09:53:19   投稿:lijiao  
這篇文章主要介紹了一個jquery驗證手機(jī)號是否正確的實踐案例,利用正則表達(dá)式進(jìn)行驗證,感興趣的小伙伴們可以參考一下

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

//聯(lián)通號碼歸屬地支持號段: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.
聯(lián)通:
2G號段(GSM):130-132、155-156;
3G號段(WCDMA):185、186.
電信:
2G號段(CDMA):133、153;
3G號段(CDMA2000):180、189.
可以寫出一個正則表達(dá)式: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>

 校驗手機(jī)號的函數(shù):

 //驗證手機(jī)號
     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 = "手機(jī)號碼不能為空!";
       }else if(phone.length !=11){
         message = "請輸入有效的手機(jī)號碼!";
       }else if(!myreg.test(phone)){
         message = "請輸入有效的手機(jī)號碼!";
       }else if(checkPhoneIsExist()){
         message = "該手機(jī)號碼已經(jīng)被綁定!";
       }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\">&nbsp;<\/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\">&nbsp;<\/i>該手機(jī)號碼可用");
       }
       return flag;
     }

發(fā)送請求給后臺:

//驗證手機(jī)號是否存在
       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后端進(jìn)行校驗:

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

以上就是本文的全部內(nèi)容,教大家如何進(jìn)行jquery驗證手機(jī)號是否正確,利用正則表達(dá)式,大家可以動手試一試。

相關(guān)文章

最新評論