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. 

 

 

可以寫出一個正則表達式: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\">&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>該手機號碼可用");
             }
             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);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM