>  기사  >  웹 프론트엔드  >  휴대폰 번호가 맞는지 확인하는 jquery의 예_jquery

휴대폰 번호가 맞는지 확인하는 jquery의 예_jquery

WBOY
WBOY원래의
2016-05-16 15:31:581276검색

휴대폰번호를 확인하려면 휴대폰번호의 번호부분을 알아야 합니다.
//휴대폰 번호 집 위치는 숫자 세그먼트를 지원합니다: 134 135 136 137 138 139 147 150 151 152 157 158 159 178 182 183 184 187 188

//China Unicom 번호 위치는 숫자 세그먼트를 지원합니다: 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})$/; >
07aefdf0967c12deeedf49513ce06cb4
먼저 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&#63;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를 사용하여 휴대폰 번호가 올바른지 확인하는 방법을 정규식을 사용하여 시도해 볼 수 있습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.